-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_capture.py
49 lines (45 loc) · 1.49 KB
/
video_capture.py
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
44
45
46
47
48
49
import cv2
import threading
import requests
import time
import os
import random
from PIL import Image
class VideoCapture():
def __init__(self, url='http://localhost:5000/api', video_source=0):
try:
os.mkdir('frames')
except:
pass
self.files = ['frames/' + x for x in os.listdir('frames')]
self.run = True
self.url = url
self.video_capture = cv2.VideoCapture(video_source)
def configure(self, video_source):
self.video_capture.release()
try:
self.video_capture = cv2.VideoCapture(video_source)
if not self.video_capture.isOpened():
print('error opening video source %s' % video_source)
exit(1)
except:
print('couldnt configure video_source')
def start(self, sleep=0.5):
while self.run:
ret, frame = self.video_capture.read()
if ret:
print('got new frame')
pil_image = Image.fromarray(frame)
f = 'frames/' + str(random.getrandbits(128)) + '.jpg'
pil_image.save(f)
self.files.append(f)
threading.Thread(target=self.send, args=(f,)).start()
time.sleep(sleep)
def send(self, f):
print('sending frame')
try:
with open(f, 'rb') as img:
r = requests.post(self.url, files={'file': img})
print(r.text)
except Exception as e:
print(e)