-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
39 lines (32 loc) · 1.25 KB
/
camera.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
import cv2
from tkinter.filedialog import asksaveasfile
from tkinter import Tk
class VideoCamera(object):
def __init__(self):
# Using OpenCV to capture from device 0. If you have trouble capturing
# from a webcam, comment the line below out and use a video file
# instead.
self.video = cv2.VideoCapture(0)
# If you decide to use video.mp4, you must have this file in the folder
# as the main.py.
# self.video = cv2.VideoCapture('video.mp4')
def __del__(self):
self.video.release()
def get_frame(self):
self.success, self.image = self.video.read()
flip = cv2.flip(self.image,1)
# We are using Motion JPEG, but OpenCV defaults to capture raw images,
# so we must encode it into JPEG in order to correctly display the
# video stream.
ret, jpeg = cv2.imencode('.jpg',flip)
return jpeg.tobytes()
def capture(self):
flip = cv2.flip(self.image,1)
files = [('JPG', '*.jpg'),
('PNG', '*.png'),
('GIF', '*.gif')]
root = Tk()
file_name = asksaveasfile(filetypes = files, defaultextension = files)
root.withdraw()
cv2.imwrite(file_name.name,flip)
return True