-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvideo.py
88 lines (72 loc) · 2.04 KB
/
video.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'''
Synopsis: Script to obtain videos from webcam.
Author: Nikhil Venkatesh
Contact: mailto:[email protected]
'''
#Opencv Imports
import cv2
import numpy as np
#Python Imports
import multiprocessing
cores_available = multiprocessing.cpu_count()
def image_capture_background(imgcap_connection):
global cap, latest_image
if imgcap_connection is None:
print ("image_capture failed because pipe is uninitialised")
return
latest_image = None
while True:
success_flag, image = cap.read()
if success_flag:
latest_image = image
if imgcap_connection.poll():
recv_obj = imgcap_connection.recv()
if recv_obj == -1:
break
imgcap_connection.send(latest_image)
cap.release()
def startCamera():
global cap, parent_conn, imgcap_conn, is_backgroundCap
is_backgroundCap=False
cap = cv2.VideoCapture(0)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
if not cap.isOpened():
print("Cannot open camera")
exit(0)
if(cores_available > 3):
print("BG process")
parent_conn, imgcap_conn = multiprocessing.Pipe()
proc = multiprocessing.Process(target=image_capture_background, args=(imgcap_conn,))
proc.daemon = True
proc.start()
is_backgroundCap = True
print("Camera is opened")
def get_frame():
global cap, is_backgroundCap, parent_conn, img_counter
img_counter =1
if(is_backgroundCap):
if(parent_conn == None):
return None
parent_conn.send(img_counter)
img_counter = img_counter + 1
img = parent_conn.recv()
img = cv2.resize(img, (200,150))
else:
success_flag, img= cap.read()
img = cv2.resize(img, (200,150))
return img
def cap_end():
global cap
print("Releasing camera")
cap.release()
if __name__ == "__main__":
startCamera()
i=1
while True:
img = get_frame()
print("Got image " +str(i))
if i==200:
break
i+=1
cap_end()