Skip to content

Instantly share code, notes, and snippets.

@dovy
Created March 29, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dovy/4fe2924ea31869def1e8 to your computer and use it in GitHub Desktop.
Save dovy/4fe2924ea31869def1e8 to your computer and use it in GitHub Desktop.
import cv2
import time
import multiprocessing
import os
start_time = time.time()
# Used when trying to thread
def process_thread(ports):
for i in ports:
init_webcam(i)
# Slow, but used to count cameras. Works, but takes as long as taking pictures.
def count_cameras():
for i in range(10):
temp_camera = cv2.VideoCapture(i-1)
retval, temp_frame = temp_camera.read()
del(temp_camera)
if temp_frame==None:
del(temp_frame)
return i-1 #MacbookPro counts embedded webcam twice
# Create the camera object and try to catch errors, but failed at catching exceptions
def init_webcam(port):
try:
cam = cv2.VideoCapture(int(port))
dir(cam)
# vars(cam)
# print(cam)
# exit()
file = "images/" + str(start_time) + "-" + str(port) + ".png"
take_picture(cam, file)
except:
print('No camera found on port %s' % str(port))
# Take the camera options, take a picture, then release the camera
def take_picture(cam, file):
retval, camera_capture = cam.read()
if camera_capture is not False: # Valid camera
if cam.isOpened() == 1:
# Needed for my camera, but maybe not for the others
# retval, camera_capture = cam.read()
cv2.imwrite(file, camera_capture)
cam.release()
# del (cam)
# else: # Let's loop and let it try again
# time.sleep(1)
# take_picture(cam, file)
def show_webcam(mirror=False):
if not os.path.exists('images'):
os.makedirs('images')
# portsA = []
# portsB = []
for i in range(0, 30):
init_webcam(str(i))
# if i % 2 == 0:
# portsA.append(str(i))
# else:
# portsB.append(str(i))
# Start thread 1
# process_thread(portsA)
# Start thread 2
# a = multiprocessing.Process(target=process_thread, args=(portsA,))
# a.start()
#
# time.sleep(1)
# b = multiprocessing.Process(target=process_thread, args=(portsB,))
# b.start()
#
#
#
# for i in portsA:
# # init_webcam(str(i))
#
# jobs.append(j)
# for j in jobs:
# j.start()
def main():
show_webcam(mirror=True)
print("--- %s seconds ---" % (time.time() - start_time))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment