-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer-vision.py
More file actions
43 lines (30 loc) · 870 Bytes
/
computer-vision.py
File metadata and controls
43 lines (30 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import torch
import cv2
import os
import time
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 14, (640, 480))
frame_count = 0
frame_files = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
results = model(frame)
results.render()
out.write(frame)
frame_filename = f'videoframes/frame_{frame_count}.jpg'
cv2.imwrite(frame_filename, frame)
frame_files.append(frame_filename)
frame_count += 1
# os.startfile(frame_filename)
time.sleep(0.05)
if os.path.exists('quit.txt'):
break
cap.release()
out.release()
for frame_file in frame_files:
if os.path.exists(frame_file):
os.remove(frame_file)