def run_camera(): global outputFrame, lock vs = WebcamVideoStream().start() while True: frame = vs.read() with lock: outputFrame = frame.copy() vs.stop()
def __init__(self, src=0, usePiCamera=False, resolution=(800, 600), framerate=30): if usePiCamera: #freak the f**k out return else: self.stream = WebcamVideoStream(src=src)
def cam(cam_index): while True: frame = WebcamVideoStream.read_frame('cam'+str(cam_index)) ret, jpeg = cv2.imencode('.jpg',frame) if jpeg is not None: yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') else: print("frame is none")
def __init__(self, src=0, usePiCamera=False, resolution=(320, 240), framerate=32): # check to see if the picamera module should be used if usePiCamera: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array] from pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src)
def gen(): while True: montages = WebcamVideoStream.read_montages() for (i, montage) in enumerate(montages): ret, jpeg = cv2.imencode('.jpg',montage) if jpeg is not None: yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n') else: print("frame is none")
def __init__(self, src=0, usePiCamera=False, resolution=(320, 240), framerate=32): # check to see if the picamera module should be used if usePiCamera: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array]` from desktops or # laptops that still want to use the `imutils` package from pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src)
def __init__(self, src=0, usePiCamera=False, resolution=(370, 290), framerate=32): if usePiCamera: from pivideostream import PiVideoStream self.stream = PiVideoStream(resolution=resolution, framerate=framerate) else: self.stream() = WebcamVideoStream(src=src)
def __init__(self, src=0, usePiCamera=False, resolution=(720, 480), framerate=32): if usePiCamera: # only import this package if using PiCamera from pivideostream import PiVideoStream self.stream = PiVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src)
class VideoStream: def __init__(self, src=0, usePiCamera=False, resolution=(800, 600), framerate=30): if usePiCamera: #freak the f**k out return else: self.stream = WebcamVideoStream(src=src) def start(self): return self.stream.start() def update(self): return self.stream.update() def read(self): return self.stream.read() def stop(self): return self.stream.stop()
def __init__(self, src=0,usePiCamera=False, resolution=(320,240),framerate=32): #check to see if the picamera module should be used if usePiCamera: from pivideostream import PiVideoStream self.stream = PiVideoStream(resolution=resolution, framerate=framerate) else: self.stream = WebcamVideoStream(src=src) def start(self): #start the threaded video stream return self.stream.start() def update(self): #grab the next frame from the stream self.stream.update() def read(self): #return the current frame return self.stream.read() def stop(self): # stop the thread and release any resources self.stream.stop()
class VideoStream: def __init__(self, src=0, usePiCamera=False, resolution=(960, 540), framerate=60): # check to see if the picamera module should be used if usePiCamera: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array]` from desktops or # laptops that still want to use the `imutils` package from pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src, resolution=resolution, framerate=framerate) def start(self): # start the threaded video stream return self.stream.start() def update(self): # grab the next frame from the stream self.stream.update() def read(self): # return the current frame return self.stream.read() def stop(self): # stop the thread and release any resources self.stream.stop()
class VideoStream: def __init__(self, src=0, usePiCamera=False, resolution=(320, 240), framerate=32): # check to see if the picamera module should be used if usePiCamera: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array]` from desktops or # laptops that still want to use the `imutils` package from pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src) def start(self): # start the threaded video stream return self.stream.start() def update(self): # grab the next frame from the stream self.stream.update() def read(self): # return the current frame return self.stream.read() def stop(self): # stop the thread and release any resources self.stream.stop()
def R(cam): print(cam) WebcamVideoStream.move_Right(cam) return '', 204
def send_img(cam): WebcamVideoStream.send_frame(cam, args.save_server) return '', 204
def events(): while True: yield "data: %s: %d, %s: %d, %s: %d, %s: %d\n\n" % (rpi_name_list[0], WebcamVideoStream.read_dnum(rpi_name_list[0]), rpi_name_list[1], WebcamVideoStream.read_dnum(rpi_name_list[1]), rpi_name_list[2], WebcamVideoStream.read_dnum(rpi_name_list[2]), rpi_name_list[3], WebcamVideoStream.read_dnum(rpi_name_list[3])) time.sleep(.1) # an artificial delay
def video_feed(): dado, img_result = predict_image(frame, od_model) return Response(gen(WebcamVideoStream().start()), mimetype='multipart/x-mixed-replace; boundary=frame')
conn = mysql.connect() cursor = conn.cursor() _hashed_password = generate_password_hash(_password) cursor.callproc('sp_createUser', (_name, _email, _hashed_password)) data = cursor.fetchall() if len(data) is 0: conn.commit() return json.dumps({'message': 'User created sucessfully!'}) else: return json.dumps({'error': str(data[0])}) else: return json.dumps( {'html': '<span>Enter the required fields !!</span>'}) @app.route('/refresh', methods=['POST']) def refresh(): return json.dumps({ 'plate': motion.plate, 'nome': motion.nome, 'carro': motion.modelo, 'cargo': motion.cargo }) if __name__ == '__main__': camera = WebcamVideoStream(src='/home/pi/camera1.mp4', measuring=True).start() motion = Movement(camera, mysql).start() app.run(host="0.0.0.0", threaded=True)
def C(cam): WebcamVideoStream.move_Init(cam) return '', 204
def D(cam): WebcamVideoStream.move_Down(cam) return '', 204
average_x = 0 average_y = 0 ################ START VIDEOSTREAM ################ # Check if we have a video or a webcam if (video_file1 is not None) and (video_file2 is not None): print("[INFO] setting up videofile...") stream1 = cv.VideoCapture(video_file1) stream2 = cv.VideoCapture(video_file2) fps1 = stream1.get(cv.CAP_PROP_FPS) fps2 = stream2.get(cv.CAP_PROP_FPS) # otherwise, we are reading from webcams else: print("[INFO] setting up webcams...") stream1 = WebcamVideoStream(src=0).start() stream2 = WebcamVideoStream(src=0).start() # Start framecount streamfps = FPS().start() streamfps = FPS().start() ######################## START CAPTURING ######################## # loop over every frame print("[INFO] starting the stream...") while (True): key = cv.waitKey(40) & 0xFF if key == ord("p"): P = np.diag([100, 100, 100, 100, 100, 100])**2 # Make the filter less uncertain if key == ord("q") or key == 27: break # quitting when ESCAPE or q is pressed
def video_feed(): return Response(gen(WebcamVideoStream().start()), mimetype='multipart/x-mixed-replace; boundary=frame')
def L(cam): WebcamVideoStream.move_Left(cam) return '', 204
class VideoStream: ##* Function Name: __init__ ##* Input: - ##* Output: - ##* Logic: Initializes the stream ##* Example Call: - def __init__(self, src=0, usePiCamera=False, resolution=(320, 240), framerate=32): # check to see if the picamera module should be used if usePiCamera: # only import the picamera packages unless we are # explicity told to do so -- this helps remove the # requirement of `picamera[array] from pivideostream import PiVideoStream # initialize the picamera stream and allow the camera # sensor to warmup self.stream = PiVideoStream(resolution=resolution, framerate=framerate) # otherwise, we are using OpenCV so initialize the webcam # stream else: self.stream = WebcamVideoStream(src=src) ##* Function Name: start ##* Input: - ##* Output: self.stream.start(), object for execution of the stream ##* Logic: Starts the stream ##* Example Call: vs=VideoStream(usePiCamera=args["picamera"] > 0).start() , here an object of Videostream class has been created, initialised and capturing of stream is started def start(self): # start the threaded video stream return self.stream.start() ##* Function Name: update ##* Input: - ##* Output: - ##* Logic: Updates the ongoing stream with every call ##* Example Call: vs.update() def update(self): # grab the next frame from the stream self.stream.update() ##* Function Name: read ##* Input: - ##* Output: self.stream.read(), captured frame ##* Logic: Returns the captured stream ##* Example Call: frame = vs.read(), reads the captured stream frame by frame def read(self): # return the current frame return self.stream.read() ##* Function Name: stop ##* Input: - ##* Output: - ##* Logic: Stops the stream ##* Example Call: vs.stop(), stops the capturing of the stream def stop(self): # stop the thread and release any resources self.stream.stop()
def present(duration=120, isTest=False): NUM_GESTURES_BEFORE_DECIDE = 10 GESTURE_BUFFER_LENGTH = 30 GESTURE_BUFFER_DETECTION_LIMIT = 0.4 detected_gestures = deque(['unknown'], GESTURE_BUFFER_LENGTH) detected_gestures_high_level = deque([], NUM_GESTURES_BEFORE_DECIDE) gesture = None round_winner = None markernames = {'rock': 1, 'scissors': 2, 'paper': 3} # Set up trial parameters n_trials = 2010 iti = 2 soa = 0.8 jitter = 0.2 record_duration = np.float32(duration) ii = 0 iii = 0 # Create markers stream outlet if not isTest: stream_info = StreamInfo('Markers', 'Markers', 1, 0, 'int32', 'myuidw12345_rsp') outlet = StreamOutlet(stream_info) # Setup trial list computer_selection = np.random.random_integers(3, size=n_trials) trials = DataFrame( dict(computer_selection=computer_selection, timestamp=np.zeros(n_trials))) # Setup graphics imgs = load_images() img = mask = mask_inv = img_question = None img_question = imgs[4][0] img_question_mask = imgs[4][1] img_question_mask_inv = imgs[4][2] #imgs["my_key"][0] #setup ranges for timing R_show_selection = range(7, 10) R_detect_player_move = range(5, 7) R_show_computer_move = range(2, 5) R_show_winner = range(1, 2) camera = WebcamVideoStream().start() #start recording start = time() main_timer = core.CountdownTimer(10) event_timestamp = None while camera.started: #for ii, trial in trials.iterrows(): # Intertrial interval #core.wait(iti + np.random.rand() * jitter) frame = camera.read() frame = cv2.bilateralFilter(frame, 5, 50, 100) # smoothing filter frame = cv2.flip(frame, 1) # flip the frame horizontally cv2.rectangle(frame, (int(cap_region_x_begin * frame.shape[1]), 0), (frame.shape[1], int(cap_region_y_end * frame.shape[0])), (255, 0, 0), 2) cv2.rectangle(frame, (0, 0), (int(cap_region_x_begin * frame.shape[1]), int(cap_region_y_end * frame.shape[0])), (25, 25, 255), 2) timer_value = main_timer.getTime() # Capture marker timestamp (with video stimulus for beginning) if event_timestamp is None: event_timestamp = time() elif timer_value > 9.5: frame = np.zeros(frame.shape, dtype=np.uint8) # # Capture marker timestamp (with audio stimulus for beginning) # if event_timestamp is None: # audio_sound.stop() # event_timestamp = time() # audio_sound.play() if int(timer_value) in R_show_selection: if int(timer_value) < 10: txt = "Rock" cv2.putText(frame, txt, (20, 470), cv2.FONT_HERSHEY_TRIPLEX, 0.8, (255, 255, 255), 2) if int(timer_value) < 9: txt = "Scissors" cv2.putText(frame, txt, (120, 470), cv2.FONT_HERSHEY_TRIPLEX, 0.8, (255, 255, 255), 2) if int(timer_value) < 8: txt = "Paper" cv2.putText(frame, txt, (240, 470), cv2.FONT_HERSHEY_TRIPLEX, 0.8, (255, 255, 255), 2) #cv2.putText(frame, txt, (30, 470), cv2.FONT_HERSHEY_TRIPLEX, 0.8, (255, 255, 255), 2) elif int(timer_value) in R_detect_player_move: #first choose computer move if img is None: # Select move label = trials['computer_selection'].iloc[ii] ii += 1 print(label) img = imgs[label][0] mask = imgs[label][1] mask_inv = imgs[label][2] else: # and display question box image roi = frame[0:hand_height, 0:hand_width] roi_bg = cv2.bitwise_and(roi, roi, mask=img_question_mask_inv) roi_fg = cv2.bitwise_and(img_question, img_question, mask=img_question_mask) dst = cv2.add(roi_bg, roi_fg) frame[0:hand_height, 0:hand_width] = dst #then detect player move if (gesture is None or len(detected_gestures_high_level) < NUM_GESTURES_BEFORE_DECIDE) and timer_value > 5.25: crop_img = frame[ cap_detection_crop_pixels:int(cap_region_y_end * frame.shape[0]) - cap_detection_crop_pixels, int(cap_region_x_begin * frame.shape[1]) + cap_detection_crop_pixels:frame.shape[1] - cap_detection_crop_pixels] grayframe = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY) tmp_gesture = capture_gesture(grayframe) if tmp_gesture != 'unknown': detected_gestures.append(tmp_gesture) if len(detected_gestures) > int( GESTURE_BUFFER_LENGTH / 2): #==detected_gestures.maxlen-1: cnt, gesture = max( map( lambda val: (detected_gestures.count(val), val), detected_gestures)) if iii == 0 or iii == 5 or iii == 10: detected_gestures_high_level.append( gesture if cnt >= int(GESTURE_BUFFER_LENGTH // 2 * GESTURE_BUFFER_DETECTION_LIMIT) else 'unknown') print(detected_gestures_high_level) print(iii) elif iii > 15: iii = 0 iii += 1 #final decision gesture = max( map(lambda val: (detected_gestures_high_level.count(val), val), set(detected_gestures_high_level)) )[1] if detected_gestures_high_level else 'detecting...' cv2.putText(frame, gesture, (int(cap_region_x_begin * frame.shape[1]) + cap_detection_crop_pixels, 470), cv2.FONT_HERSHEY_TRIPLEX, 0.8, (255, 255, 255), 2) #if we detected the gesture with high confidentiality , skip this part by altering the timer if gesture in ( 'rock', 'scissors', 'paper' ) and detected_gestures_high_level.count(gesture) > int( detected_gestures_high_level.maxlen * GESTURE_BUFFER_DETECTION_LIMIT): main_timer.add(-1 * (timer_value - 5)) elif int(timer_value) in R_show_computer_move: #send marker if (not isTest) and (gesture in ('rock', 'scissors', 'paper')): outlet.push_sample([markernames[gesture]], event_timestamp) # show chosen computer move roi = frame[0:hand_height, 0:hand_width] roi_bg = cv2.bitwise_and(roi, roi, mask=mask_inv) roi_fg = cv2.bitwise_and(img, img, mask=mask) dst = cv2.add(roi_bg, roi_fg) frame[0:hand_height, 0:hand_width] = dst elif int(timer_value) in R_show_winner: #show winner if round_winner == None: if gesture not in ('rock', 'scissors', 'paper'): round_winner = 'Invalid Match' else: round_winner = 'Player' if ( label == 1 and gesture == 'paper' ) or (label == 2 and gesture == 'rock') or ( label == 3 and gesture == 'scissors') else 'Computer' if (label == 1 and gesture == 'rock') or ( label == 2 and gesture == 'scissors') or ( label == 3 and gesture == 'paper'): round_winner = 'Draw' result_msg_x_pos = int( cap_region_x_begin * frame.shape[1] ) + cap_detection_crop_pixels if round_winner == 'Player' else 30 if round_winner in ('Draw', 'Invalid Match'): cv2.putText(frame, round_winner, (240, 470), cv2.FONT_HERSHEY_TRIPLEX, 0.8, (255, 255, 255), 3) else: cv2.putText(frame, 'WINNER', (result_msg_x_pos, 470), cv2.FONT_HERSHEY_TRIPLEX, 0.8, (255, 255, 255), 3) elif timer_value < 0: #clear variables iii = 0 img = mask = mask_inv = None gesture = None round_winner = None event_timestamp = None detected_gestures_high_level.clear() detected_gestures.clear() detected_gestures.append('unknown') main_timer.reset() cv2.imshow('original', frame) # Send marker #timestamp = time() #outlet.push_sample([markernames[label]], timestamp) #mywin.flip() # offset #core.wait(soa) #mywin.flip() k = cv2.waitKey(10) & 0xFF if (k == 27) or (time() - start) > record_duration: break #event.clearEvents() # Cleanup & exiting camera.stop() cv2.destroyAllWindows()
def main(): center = deque(maxlen=2) center.appendleft((0, 0)) center.appendleft((0, 0)) leftStick = Stick("left") rightStick = Stick("right") # Upper and lower bounds (HSV) for the stick color objLower = (30, 86, 14) objUpper = (97, 244, 255) frameCount = 0 vs = WebcamVideoStream(src=0).start() #vs = FileVideoStream(0).start() time.sleep(1.0) while True: # Read in 1 frame at a time and flip the image frame = vs.read() #frame = imutils.resize(frame, width = 600, height = 300) frame = cv2.flip(frame, 1) overlay = frame.copy() alpha = 0.5 cv2.line(overlay, (150, 0), (150, 600), (138, 138, 138), 1) cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame) cv2.line(frame, (450, 0), (450, 600), (138, 138, 138), 1) # Mask the image so the result is just the drum stick tips hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.inRange(hsv, objLower, objUpper) mask = cv2.erode(mask, None, iterations=1) # Find contours in the mask cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # sort cnts so we can loop through the two biggest (the sticks hopefully) cnts = sorted(cnts, key=lambda x: cv2.contourArea(x), reverse=True) numSticks = min(len(cnts), 2) for i in range(numSticks): ((x, y), radius) = cv2.minEnclosingCircle(cnts[i]) if (radius > 4): center.appendleft((int(x), int(y))) for i in range(numSticks): if (numSticks > 1): if (center[i][0] <= center[(i + 1) % 2][0]): cv2.circle(frame, center[i], 10, (156, 76, 76), 3) leftStick.addPoint(center[i][0], center[i][1]) if (frameCount > 4): trackStick(leftStick) else: cv2.circle(frame, center[i], 10, (76, 76, 156), 3) rightStick.addPoint(center[i][0], center[i][1]) if (frameCount > 4): trackStick(rightStick) # Only one stick - split screen in half else: if (center[i][0] >= 300): leftStick.addPoint(center[i][0], center[i][1]) if (frameCount > 4): trackStick(leftStick) else: rightStick.addPoint(center[i][0], center[i][1]) if (frameCount > 4): trackStick(rightStick) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF frameCount += 1 # if the 'q' key is pressed, stop the loop if key == ord("q"): break vs.stop() cv2.destroyAllWindows()
def video_feed(): """Video streaming route. Put this in the src attribute of an img tag.""" return Response(gen(WebcamVideoStream().start()), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/R/<string:cam>') def R(cam): print(cam) WebcamVideoStream.move_Right(cam) return '', 204 @app.route('/L/<string:cam>') def L(cam): WebcamVideoStream.move_Left(cam) return '', 204 @app.route('/U/<string:cam>') def U(cam): WebcamVideoStream.move_Up(cam) return '', 204 @app.route('/D/<string:cam>') def D(cam): WebcamVideoStream.move_Down(cam) return '', 204 @app.route('/C/<string:cam>') def C(cam): WebcamVideoStream.move_Init(cam) return '', 204 if __name__ == '__main__': WebcamVideoStream().start() app.run(host='0.0.0.0', debug=False, threaded=True)
# update the FPS counter fps.update() # stop the timer and display FPS info fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # cleanup stream.release() cv2.destroyAllWindows() # create a threaded video stream, use FPS counter print("[INFO] sampling THREADED frames from a webcam...") vs = WebcamVideoStream(src=0).start() fps = FPS().start() # ************this is not working********** # I believe this is a windows issue, this is for # the raspberry pi, so will test on there before # doing too much more here. # loop over frames using threaded stream while fps._numFrames < args["num_frames"]: # grab frame and resize to 400 px frame = vs.read() frame = imutils.resize(frame, width=400) # check to see if the frame should be displayed
def U(cam): WebcamVideoStream.move_Up(cam) return '', 204
xe = None ye = None lastframe = None ################### START VIDEOSTREAM ################### print("[INFO] sampling frames from webcam...") # Check if we have a video or a webcam if video_file is not None: stream = cv.VideoCapture(video_file) fps = stream.get(cv.CAP_PROP_FPS) # otherwise, we are reading from a webcam else: stream = WebcamVideoStream(src=0).start() # measurements streamfps = FPS().start() out = cv.VideoWriter('outframe.avi', cv.VideoWriter_fourcc('M', 'J', 'P', 'G'), 15, (frame_w, frame_h)) ################### START CAPTURING ################### # loop over every frame while (True): key = cv.waitKey(40) & 0xFF #if key== ord("c"): crop = True # Crop only to the region of interest if key == ord("p"): P = np.diag([100, 100, 100, 100])**2 # Make the filter uncertain again if key == ord("q") or key == 27: break # quitting when ESCAPE or q is pressed
def __init__(self): self.stream = WebcamVideoStream(src=0)