class Video(): def __init__(self): self.vs = VideoStream(usePiCamera=1 > 0).start() time.sleep(2.0) self.currentFrame = np.array([]) self.raw_img = np.array([]) def captureRawFrame(self): """ capture frame and reverse RBG BGR and return opencv image """ rawFrame = self.vs.read() rawFrame = imutils.resize(rawFrame, width=640) self.raw_img = rawFrame #return rawFrame def convertFrame(self): """ converts frame to format suitable for QtGui """ try: self.currentFrame = cv2.cvtColor(self.raw_img, cv2.COLOR_BGR2RGB) height, width = self.currentFrame.shape[:2] img = QtGui.QImage(self.currentFrame, width, height, QtGui.QImage.Format_RGB888) img = QtGui.QPixmap.fromImage(img) #self.previousFrame = self.currentFrame img = img.scaledToHeight(480) img = img.scaledToWidth(360) return img except: return None
def __init__(self): # initialize the video stream and allow the camera # sensor to warmup self.vs = VideoStream(usePiCamera=1 > 0).start() time.sleep(2.0) self.currentFrame = np.array([]) self.raw_img = np.array([]) self.writer = None (h, w) = (None, None)
def main(): global frame, key # initialize the camera and grab a reference to the raw camera capture wdth = int(math.floor(360)) hgth = int(math.floor(800)) camera = VideoStream(usePiCamera=True,resolution=(wdth,hgth)).start() time.sleep(2.0) fourcc = cv2.VideoWriter_fourcc(*'MJPG') writer = None (h,w) = (None, None) # setup the mouse callback cv2.startWindowThread() cv2.namedWindow("Detection") cv2.setMouseCallback("Detection",mouseOn) # keep looping over the frames #for frame2 in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): while True: frame = camera.read(); frame = cv2.transpose(frame); frame = cv2.flip(frame,1) timestamp = datetime.datetime.now() ts = timestamp.strftime("%d/%m/%Y %H:%M:%S") cv2.putText(frame,ts,(10,frame.shape[0]-10),cv2.FONT_HERSHEY_SIMPLEX,0.35,(0,255,0),1) if writer is None: (h,w) = frame.shape[:2] writer = cv2.VideoWriter("/media/usb/test_" + timestamp.strftime("%d_%m_%Y_%H%M") + ".avi", fourcc,5,(w,h), True) writer.write(frame) cv2.imshow("Detection", frame); #cv2.setMouseCallback("Detection",mouseOn) #key = cv2.waitKey(10) & 0xFF # if the 'q' key is pressed, stop the loop if key == ord("q"): #cv2.EVENT_LBUTTONDOWN: #ord("q"): # cv2.destroyAllWindows() # camera.stop() break # cleanup the camera and close any open windows cv2.destroyAllWindows() camera.stop()
class recordVideo(): def __init__(self): # initialize the video stream and allow the camera # sensor to warmup self.vs = VideoStream(usePiCamera=1 > 0).start() time.sleep(2.0) self.currentFrame = np.array([]) self.raw_img = np.array([]) self.writer = None (h, w) = (None, None) def captureRawFrame(self): """ capture frame and reverse RBG BGR and return opencv image, and also record the video """ rawFrame = self.vs.read() rawFrame = imutils.resize(rawFrame, width=640) self.raw_img = rawFrame #return rawFrame def initRecord(self): if self.writer == None: # store the image dimensions, initialzie the video writer, # and construct the zeros array #(h, w) = self.raw_img.shape[:2] self.writer = cv2.VideoWriter('./demoVideo/'+str(int(time.time()))+'.avi', cv2.cv.FOURCC(*"XVID"), 15, (640 , 480 ), True) def record(self): # write the output frame to file self.writer.write(self.raw_img) def convertFrame(self): """ converts frame to format suitable for QtGui """ try: self.currentFrame = cv2.cvtColor(self.raw_img, cv2.COLOR_BGR2RGB) height, width = self.currentFrame.shape[:2] img = QtGui.QImage(self.currentFrame, width, height, QtGui.QImage.Format_RGB888) img = QtGui.QPixmap.fromImage(img) #self.previousFrame = self.currentFrame img = img.scaledToHeight(480) img = img.scaledToWidth(360) return img except: return None
def getData(): # # construct the argument parse and parse the arguments # ap = argparse.ArgumentParser() # ap.add_argument("-v", "--video", # help="path to the (optional) video file") # ap.add_argument("-b", "--buffer", type=int, default=64, # help="max buffer size") # args = vars(ap.parse_args()) args = {'video': '', 'buffer': 300} # define the lower and upper boundaries of the "green" # ball in the HSV color space, then initialize the # list of tracked points greenLower = (29, 86, 6) greenUpper = (64, 255, 255) pts = deque(maxlen=args["buffer"]) # if a video path was not supplied, grab the reference # to the webcam if not args.get("video", False): vs = VideoStream(src=0).start() # otherwise, grab a reference to the video file else: vs = cv2.VideoCapture(args["video"]) # allow the camera or video file to warm up time.sleep(2.0) # keep looping while True: # grab the current frame frame = vs.read() # handle the frame from VideoCapture or VideoStream frame = frame[1] if args.get("video", False) else frame # if we are viewing a video and we did not grab a frame, # then we have reached the end of the video if frame is None: break # resize the frame, blur it, and convert it to the HSV # color space frame = imutils.resize(frame, width=600) blurred = cv2.GaussianBlur(frame, (11, 11), 0) hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) # construct a mask for the color "green", then perform # a series of dilations and erosions to remove any small # blobs left in the mask mask = cv2.inRange(hsv, greenLower, greenUpper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) # find contours in the mask and initialize the current # (x, y) center of the ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) center = None # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # only proceed if the radius meets a minimum size if radius > 10: # draw the circle and centroid on the frame, # then update the list of tracked points cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) # update the points queue pts.appendleft(center) # loop over the set of tracked points for i in range(1, len(pts)): # if either of the tracked points are None, ignore # them if pts[i - 1] is None or pts[i] is None: continue # otherwise, compute the thickness of the line and # draw the connecting lines thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5) cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness) # show the frame to our screen cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the 'q' key is pressed, stop the loop if key == ord("q"): break # if we are not using a video file, stop the camera video stream if not args.get("video", False): vs.stop() # otherwise, release the camera else: vs.release() # close all windows cv2.destroyAllWindows()
import time import cv2 import imutils from imutils.video import VideoStream from matcher import Matcher matcher = Matcher([("fau-logo", "./templates/fau-logo.png"), ("first-logo", "./templates/first-logo.jpg"), ("nextera-logo", "./templates/nextera-energy-logo.jpg"), ("techgarage-logo", "./templates/techgarage-logo.png") ], min_keypoints_pct_match=8) cam = VideoStream(usePiCamera=False).start() cnt = 0 while True: img = cam.read() cv2.imshow("Pic", img) print matcher.match(img) key = cv2.waitKey(10) if key == ord('q'): break cam.stop() cv2.destroyAllWindows()
# import the necessary packages import datetime import time import cv2 from imutils.video import VideoStream import imutils # will use the RPi camera by default. # vs = VideoStream().start() stream_url = "rtsp://192.168.0.176:554/11" vs = VideoStream(stream_url).start() time.sleep(0.1) # Loop over the frames from the video stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=1080) # draw the timestamp on the frame timestamp = datetime.datetime.now() ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p") cv2.putText( frame, ts, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35,
import imagezmq import argparse import socket import time # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-s", "--server-ip", required=True, help="ip address of the server to which the client will connect") args = vars(ap.parse_args()) # initialize the ImageSender object with the socket address of the # server sender = imagezmq.ImageSender(connect_to="tcp://{}:5555".format( args["server_ip"])) # get the host name, initialize the video stream, and allow the # camera sensor to warmup rpiName = socket.gethostname() vs = VideoStream(usePiCamera=True, framerate=20).start() #vs = VideoStream(src=0).start() time.sleep(2.0) while True: # read the frame from the camera and send it to the server frame = vs.read() sender.send_image(rpiName, frame) #hub_reply = sender.zmq_socket.recv() #if hub_reply == "DONE": # break
# initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("Loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] print("Starting live video stream...") vs = VideoStream(src=0).start() fileStream = False time.sleep(1.0) currentCount = 0 mouse = Mouse() face_cascade = cv2.CascadeClassifier('res/haarcascade_frontalface_default.xml') while True: # if this is a file video stream, then we need to check if # there any more frames left in the buffer to process if fileStream and not vs.more(): break
import sys import time from imutils.video import VideoStream import imutils # define the lower and upper boundaries of the red in HSV redLower1 = (0, 100, 100) redUpper1 = (10, 255, 255) redLower2 = (160, 100, 100) redUpper2 = (179, 255, 255) # initialize the list of tracked points, the frame counter, # and the coordinate deltas (dX, dY) = (0, 0) video_stream = VideoStream(usePiCamera=False, resolution=(640,480), framerate=32).start() time.sleep(2) # keep looping while True: # grab the current frame # image = video_stream.read() frame = video_stream.read() # resize the frame, blur it, and convert it to the HSV # color space # frame = imutils.resize(image, width=400) blurred = cv2.GaussianBlur(frame, (11, 11), 0) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # construct a mask for the color "green", then perform
COUNTER = 0 ALARM_ON = False # initialize dlib's face detector and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] print("[INFO] starting video stream thread...") vs = VideoStream(src=args["webcam"]).start() time.sleep(1.0) # loop over frames from the video stream while True: # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale frame = vs.read() frame = imutils.resize(frame, width=500) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) # loop over the face detections for rect in rects:
def viewCameraPi(self): #text = 'This is a message from app to inform that app start running now!' #statusSMS = outboundSMSviaTwilio(account=self.account, token=self.token, destPhone=self.destPhone1, # twilioNumber=self.twilioNumber, message_body=text) #statusSMS = outboundSMSviaTwilio(account=self.account, token=self.token, destPhone=self.destPhone2, # twilioNumber=self.twilioNumber, message_body=text) print("START SCRIPT AND MAJOR WARNING!") statusSMS = 'delivered' if((statusSMS != 'failed') and (statusSMS != 'undelivered')): #camera = PiCamera() #camera.resolution = ( 640, 480) #camera.framerate = 32 #rawCapture = PiRGBArray(camera, size=( 640, 480)) #self.sumMSE = self.sumSSIM = self.avgMSE = self.avgSSIM = 0 self.tempHour = datetime.datetime.now().hour self.tempMinute = datetime.datetime.now().minute self.warmup = 0 vs = VideoStream(usePiCamera=1,resolution=(640,480)).start() time.sleep(1.2) fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = None (h, w) = (None, None) zeros = None print ("view camera") while True: self.warmup+=1 if(self.warmup >=5): frame = vs.read() if writer is None: # store the image dimensions, initialzie the video writer, # and construct the zeros array (h, w) = frame.shape[:2] writer = cv2.VideoWriter('exampleTH3.avi', fourcc, 20, (w, h), True) writer.write(frame) self.curImage = frame self.getDefImagePerHours() #write xml print("process frame thu {}".format(self.warmup-4)) self.writeXML() #tat chuong trinh sau 5 phut: if(datetime.datetime.now().minute - self.tempMinute >5): self.final() break # for frame in camera.capture_continuous( rawCapture, format("bgr"), use_video_port = True): # # self.curImage = frame.array # frame = vs.read() # self.warmup +=1 # if(self.warmup >=5): # self.getDefImagePerHours() # # self.getDefImagePerTenMinutes() # # #write xml # print("process frame thu {}".format(self.warmup-4)) # self.writeXML() # # #warning # # self.warning() # # #tat chuong trinh sau 5 phut: # if(datetime.datetime.now().minute - self.tempMinute >5): # self.final() # break # #neu la 16h, script se tu tat # # tempBreak = datetime.datetime.now().hour # # if(( tempBreak == 0) or (tempBreak == 6) or (tempBreak == 18) or (tempBreak == 12)): # # if(datetime.datetime.now().minute == 0): # # if((datetime.datetime.now().second >= 0) and (datetime.datetime.now().second <=3)): # # self.final() # # self.__init__(tempBreak) # # # show frame # # cv2.imshow("image", self.curImage) # # key = cv2.waitKey(1) & 0xFF # # #renew # rawCapture.truncate(0) # # #press 'q' to stop, press any key to continue # # if(key == ord("q")): # # break #call function final vs.stop() writer.release() self.final() else : print("send message failed. So App will not run. Sorry for the inconvenience!!")
tracker = CentroidTracker() H, W = None, None # Pre-trained Deep Learning Detector import os print('Config File found: ', os.path.exists(args['prototxt'])) print('Model File found: ', os.path.exists(args['model'])) print('Loading Config File... ', args['prototxt']) print('Loading Model File... ', args['model']) net = cv2.dnn.readNetFromCaffe(args['prototxt'], args['model']) # VIDEO CAPTURE print('Opening webcam...') video = VideoStream(src=0).start() time.sleep(2.0) # h,w = video.resolution h, w = (225, 400) print('Start recording...') fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') cam = cv2.VideoWriter('output.mp4', fourcc, 20.0, (h, w)) while True: # 1 - Start video frame = video.read() frame = imutils.resize(frame, width=400) # 1.2 - Grap frame dimensions
# initialize the list of class labels MobileNet SSD was trained to # detect, then generate a set of bounding box colors for each class CLASSES = [" ", "30", "school"] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromTensorflow("frozen_inference_graph.pb", "warning.pbtxt") # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") vs = VideoStream(src=0, resolution=(640, 480), framerate=500).start() time.sleep(2.0) fps = FPS().start() # loop over the frames from the video stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=480) # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (640, 480)), size=(640, 480), swapRB=True)
import cv2 import numpy as np from imutils.video import VideoStream from imutils import resize diff_threshold = 1000000 vs = VideoStream(src=0).start() def getImage(): im = vs.read() im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) im = cv2.blur(im, (20, 20)) return im old_image = getImage() while True: new_image = getImage() diff = cv2.absdiff(old_image, new_image) diff_score = np.sum(diff) # print(diff_score) if diff_score > diff_threshold: print("Movement detected") old_image = new_image
import winsound frequency = 2500 # Set Frequency To 2500 Hertz duration = 800 # Set Duration To 1000 ms == 1 second ap = argparse.ArgumentParser() ap.add_argument("-o", "--output", type=str, default="barcodesData.csv", help="path to output CSV file ") args = vars(ap.parse_args()) print("Starting webcam") vs = VideoStream(src=0).start() time.sleep(2.0) csvWrite = open(args["output"], "w") found = set() while True: frameData = vs.read() frameData = imutils.resize(frameData, width=600) barcodes = pyzbar.decode(frameData) for barcode in barcodes: (x, y, width, height) = barcode.rect cv2.rectangle(frameData, (x, y), (x + width, y + height), (0, 0, 255), 2) barcodeData = barcode.data.decode("utf-8") barcodeType = barcode.type textData = "{} ({})".format(barcodeData, barcodeType) cv2.putText(frameData, textData, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
from imutils.video import VideoStream from imutils.video import FPS import numpy as np import imutils from time import sleep import cv2 vs = VideoStream(src=0).start() sleep(2) fps = FPS().start() frame1 = vs.read() frame1 = imutils.resize(frame1, width=400) prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) hsv = np.zeros_like(frame1) hsv[...,1] = 255 while True: frame2 = vs.read() frame2 = imutils.resize(frame2, width=400) next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 3, 15, 3, 5, 1.2, 0) mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1]) hsv[...,0] = ang*180/np.pi/2 hsv[...,2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) cv2.imshow('frame2', bgr) k = cv2.waitKey(30) & 0xff
else: if args['rangefilter'].upper() == "RGB": print("Error! RGB is currently unsupported, sorry!") else: color_range = args['color'] colorLower = color_range[0:3] colorUpper = color_range[3:6] video_extensions = ("3g2", "3gp", "asf", "asx", "avi", "flv", "m4v", "mov", "mp4", "mpg", "rm", "swf", "vob", "wmv") if str(source.endswith(video_extensions)): video = True else: video = False # created a threaded video stream vs = VideoStream(src=args["source"]).start() while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels (grabbed, frame) = vs.read() if video and not grabbed: break # resize the frame, blur it, and convert it to the HSV # color space frame = imutils.resize(frame, width=600) blurred = cv2.GaussianBlur(frame, (11, 11), 0) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # construct a mask for the set color, then perform
import cv2 import imutils from imutils.video import VideoStream from imutils import face_utils import time import vlc import random print("[INFO] camera sensor warming up...") vs = VideoStream(usePiCamera=0).start() time.sleep(2.0) while True: frame = vs.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) detector = cv2.CascadeClassifier("./res/Stopsign_HAAR_19Stages.xml") rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) for (x, y, w, h) in rects: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) frame = imutils.resize(frame, width=400) cv2.imshow("Frame", frame) cv2.waitKey(1)
def initialize_with_webcam(self): if self.video_file is not None or self.camera is not None or self.picamera is not None: raise TEVideoException("Already Initialized") self.camera = VideoStream().start() time.sleep(2.0)
# File: threaded_videostream_demo.py import time import cv2 import numpy as np from imutils.video import VideoStream import imutils # Are we using the Pi Camera? usingPiCamera = True # Set initial frame size. frameSize = (320, 240) # Initialize mutithreading the video stream. vs = VideoStream(src=0, usePiCamera=usingPiCamera, resolution=frameSize, framerate=32).start() # Allow the camera to warm up. time.sleep(2.0) timeCheck = time.time() while True: # Get the next frame. frame = vs.read() # If using a webcam instead of the Pi Camera, # we take the extra step to change frame size. if not usingPiCamera: frame = imutils.resize(frame, width=frameSize[0]) # Show video stream cv2.imshow('orig', frame)
def __init__(self): self.root = Tk() self.root.wm_title("Dual Cam") self.saving = False self.frames = [] self.overlay = False self.image_left = ImageTk.PhotoImage(image=Image.fromarray(np.uint8(np.zeros((256, 256))))) self.image_panel_left = Label(self.root, image = self.image_left) self.image_panel_left.grid(row = 0, column = 0, columnspan=2) self.image_right = ImageTk.PhotoImage(image=Image.fromarray(np.uint8(256 * np.random.rand(256, 256)))) self.image_panel_right = Label(self.root, image = self.image_right) self.image_panel_right.grid(row = 0, column = 2, columnspan=2) self.save_button = Button(width = 10, height = 2, text = 'Save', command=self.save) self.save_button.grid(row = 1, column = 0) self.calibrate_button = Button(width = 10, height = 2, text = 'Calibrate', command=self.calibrate) self.calibrate_button.grid(row = 1, column = 1) self.close_button = Button(width = 10, height = 2, text = 'Close', command=self.quit) self.close_button.grid(row = 1, column = 3) self.overlay_button = Button(width=10, height=2, text='Overlay', command=self.toggle_overlay) self.overlay_button.grid(row = 1, column = 2) self.bias_slider = Scale(self.root, from_=0, to=31, length=400, orient=HORIZONTAL, command=self.bias) self.bias_slider.grid(row = 2, column = 1, columnspan=3) self.bias_label = Label(self.root, text="Bias current") self.bias_label.grid(row=2, column=0) self.clock_slider = Scale(self.root, from_=0, to=63, length=400, orient=HORIZONTAL, command=self.clock) self.clock_slider.grid(row = 3, column = 1, columnspan=3) self.clock_label = Label(self.root, text="Clock speed") self.clock_label.grid(row=3, column=0) self.cm_slider = Scale(self.root, from_=0, to=31, length=400, orient=HORIZONTAL, command=self.cm) self.cm_slider.grid(row = 4, column = 1, columnspan=3) self.cm_label = Label(self.root, text="CM current") self.cm_label.grid(row=4, column=0) # set default positions self.cm_slider.set(0x0C) self.clock_slider.set(0x15) self.bias_slider.set(0x05) # initialize visible camera self.vs = VideoStream(usePiCamera=True).start() # thread for reading from sensor hardware intro an image queue self.ir_images = Queue.LifoQueue() self.ir_commands = Queue.Queue() self.ir_calibrate = threading.Event() self.ir_stop = threading.Event() self.raw_ir_images = Queue.LifoQueue() self.capture_thread = threading.Thread( target=ir_capture, name="capture_thread", args=[self.ir_images, self.ir_calibrate, self.ir_stop, self.ir_commands, self.raw_ir_images] ) self.capture_thread.start() self.ticktock() self.root.mainloop()
class FaceDetectorPredictor: def __init__(self): ''' # Creating 'object' for Database Updation self.DbU = DatabaseUpdater() # Creating Connection with Database self.DbU.createConnection("attendance") # Obtaing Roll Numbers from the database self.DbU.getRollNos("Sub1") ''' # Initializing paths for 'prototxt', 'model' and 'confidence' prototxt_path = "TrainingEntities/deploy.prototxt" model_path = "TrainingEntities/res10_300x300_ssd_iter_140000.caffemodel" self.threshold_confidence = 0.14 # Load Serialized Model from Disk self.modal = cv2.dnn.readNetFromCaffe(prototxt_path, model_path) # Starting WebCam self.vCap = VideoStream(src=0).start() time.sleep(2.0) # load YAML and create model yaml_file = open('TrainedEntities/modal.yaml', 'r') loaded_model_yaml = yaml_file.read() yaml_file.close() self.loaded_model = model_from_yaml(loaded_model_yaml) # load weights into new model self.loaded_model.load_weights("TrainedEntities/modal.h5") print("Loaded model from disk") self.loaded_model.compile(loss='categorical_crossentropy', optimizer='adadelta') def FaceDetector(self): self.flag = 0 # Loop over the Frames from the Video Stream hhh = 0 while True: capturedFrame = cv2.imread("abc.jpg") # Reading returned captured frame Image #capturedFrame = self.vCap.read() #capturedFrame = cv2.imread("abc.jpg")#cv2.imread("abc.jpg") a, b, c = np.mean(capturedFrame, axis=(0, 1)) a = int(a) * 1.0 b = int(b) * 1.0 c = int(c) * 1.0 newFrame = imutils.resize(capturedFrame, width=700) # some edititng # Grab the Frame Dimensions and convert it to a 'Blob' (h, w) = newFrame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(newFrame, (300, 300)), 1.0, (300, 300), (a, b, c)) # Pass the 'Blob' through the network and obtain the 'Detections' and 'Predictions' self.modal.setInput(blob) detections = self.modal.forward() # Loop over all the Detections for i in range(0, detections.shape[2]): # Extract the 'confidence' (i.e., probability) associated with the prediction confidence = detections[0, 0, i, 2] # Filter out weak detections by ensuring the 'confidence' is greater than the minimum confidence if confidence < self.threshold_confidence: continue # Compute the (X, Y)-coordinates of the Bounding Box for the Object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (self.startX, self.startY, self.endX, self.endY) = box.astype("int") # Draw the Bounding Box of the face along with the Associated Probability confidencePercentage = "{:.2f}%".format(confidence * 100) if ((self.startY - 10) > 10): y = self.startY - 10 else: y = self.startY + 10 grayFrame = cv2.cvtColor(newFrame, cv2.COLOR_BGR2GRAY) #print("Values", self.startX, self.startY, self.endX, self.endY, "shown,", h, w, "hw shown") if (self.startX > 0 and self.startY > 0): if (self.endX < 700 and self.endY < 525): faceFrame = grayFrame[self.startY:self.endY, self.startX:self.endX]; #cv2.imshow("frr",cv2.resize(faceFrame, (224, 244))) faceFrame = cv2.resize(faceFrame, (224, 224)) cv2.imwrite("NewDataset/" + "User_" + str(hhh) + ".jpg",faceFrame) hhh += 1 #faceFrame = faceFrame[newaxis, :224, :224, newaxis] #faceFrame = np.array(faceFrame) #faceFrame = faceFrame.reshape((faceFrame.shape[0],224,224,1).astype('float32')) faceFrame = faceFrame.reshape(1,224,224,1) Id = "helo" Id = self.loaded_model.predict(faceFrame) print(Id) ''' maxx = Id[0][0] j = -1 for i in Id[0]: print(i) j += 1 if int(i) > int(maxx): maxx = j break ''' #print(Id) cv2.rectangle(newFrame, (self.startX, self.startY), (self.endX, self.endY), (0, 255, 0), 2) #cv2.putText(newFrame, Id, (self.startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2) # Showing the captured frame image cv2.imshow("Frame", newFrame) # cv2.imwrite("output.jpg", newFrame) # Checking which key was pressed keyPressed = cv2.waitKey(1) if (keyPressed == ord("q") or cv2.waitKey(1) == ord("Q")): break # Exiting Camera self.vCap.stop() # Destroying All Windows cv2.destroyAllWindows()
def __init__(self): self.vs = VideoStream(usePiCamera=1 > 0).start() time.sleep(2.0) self.currentFrame = np.array([]) self.raw_img = np.array([])
def face_mask(): def detect_and_predict_mask(frame, faceNet, maskNet): # grab the dimensions of the frame and then construct a blob # from it (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 1.0, (224, 224), (104.0, 177.0, 123.0)) # pass the blob through the network and obtain the face detections faceNet.setInput(blob) detections = faceNet.forward() print(detections.shape) # initialize our list of faces, their corresponding locations, # and the list of predictions from our face mask network faces = [] locs = [] preds = [] # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the detection confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the confidence is # greater than the minimum confidence if confidence > 0.5: # compute the (x, y)-coordinates of the bounding box for # the object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # ensure the bounding boxes fall within the dimensions of # the frame (startX, startY) = (max(0, startX), max(0, startY)) (endX, endY) = (min(w - 1, endX), min(h - 1, endY)) # extract the face ROI, convert it from BGR to RGB channel # ordering, resize it to 224x224, and preprocess it face = frame[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) face = cv2.resize(face, (224, 224)) face = img_to_array(face) face = preprocess_input(face) # add the face and bounding boxes to their respective # lists faces.append(face) locs.append((startX, startY, endX, endY)) # only make a predictions if at least one face was detected if len(faces) > 0: # for faster inference we'll make batch predictions on *all* # faces at the same time rather than one-by-one predictions # in the above `for` loop faces = np.array(faces, dtype="float32") preds = maskNet.predict(faces, batch_size=32) # return a 2-tuple of the face locations and their corresponding # locations return (locs, preds) # load our serialized face detector model from disk prototxtPath = r"face_detector\deploy.prototxt" weightsPath = r"face_detector\res10_300x300_ssd_iter_140000.caffemodel" faceNet = cv2.dnn.readNet(prototxtPath, weightsPath) # load the face mask detector model from disk maskNet = load_model("mask_detector.model") # initialize the video stream print("[INFO] starting video stream...") engine = pyttsx3.init() engine.say("please put your mask on ") engine.runAndWait() vs = VideoStream(src=0).start() # loop over the frames from the video stream condition = True while condition: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=400) # detect faces in the frame and determine if they are wearing a # face mask or not (locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet) # loop over the detected face locations and their corresponding # locations for (box, pred) in zip(locs, preds): # unpack the bounding box and predictions (startX, startY, endX, endY) = box (mask, withoutMask) = pred # determine the class label and color we'll use to draw # the bounding box and text label = "Mask" if mask > withoutMask else "No Mask" color = (0, 255, 0) if label == "Mask" else (0, 0, 255) # include the probability in the label label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100) # display the label and bounding box rectangle on the output # frame cv2.putText(frame, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2) cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2) col = color if col == (0, 255, 0): print("mask on") condition = False cv2.destroyAllWindows() # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
# python realtime_stitching.py # import the necessary packages from __future__ import print_function from pyimagesearch.basicmotiondetector import BasicMotionDetector from pyimagesearch.panorama import Stitcher from imutils.video import VideoStream import numpy as np import datetime import imutils import time import cv2 # initialize the video streams and allow them to warmup print("[INFO] starting cameras...") leftStream = VideoStream(src=1).start() #rightStream = VideoStream(usePiCamera=True).start() rightStream = VideoStream(src=0).start() time.sleep(2.0) # initialize the image stitcher, motion detector, and total # number of frames read stitcher = Stitcher() motion = BasicMotionDetector(minArea=500) total = 0 # loop over frames from the video streams while True: # grab the frames from their respective video streams left = leftStream.read() right = rightStream.read()
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-m', '--model', type=str, required=True, help='File path of .tflite file.') parser.add_argument('-l', '--labels', type=str, required=True, help='File path of labels file.') parser.add_argument('-t', '--threshold', type=float, default=0.4, required=False, help='Score threshold for detected objects.') parser.add_argument('-p', '--picamera', action='store_true', default=False, help='Use PiCamera for image capture') parser.add_argument('-e', '--use_edgetpu', action='store_true', default=False, help='Use EdgeTPU') args = parser.parse_args() labels = load_labels(args.labels) interpreter = make_interpreter(args.model, args.use_edgetpu) interpreter.allocate_tensors() _, input_height, input_width, _ = interpreter.get_input_details( )[0]['shape'] # Initialize video stream vs = VideoStream(usePiCamera=args.picamera, resolution=(640, 480)) vs.start( ) # this would close/ release the camera object properly. For vs = VideoStream().start(), vs.stop() would not work time.sleep(1) fps = FPS().start() while True: try: # Read frame from video screenshot = vs.read() image = Image.fromarray(screenshot) image_pred = image.resize((input_width, input_height), Image.ANTIALIAS) # Perform inference results = detect_objects(interpreter, image_pred, args.threshold) draw_image(image, results, labels, image.size) if (cv2.waitKey(5) & 0xFF == ord('q')): fps.stop() break fps.update() except KeyboardInterrupt: fps.stop() break print("Elapsed time: " + str(fps.elapsed())) print("Approx FPS: :" + str(fps.fps())) cv2.destroyAllWindows() vs.stop() time.sleep(2)
def camera_thread(): global key, ra, dec, killFlag, error_in_deg_h, error_in_deg_v global gray global mouseX, mouseY global update_tracker dx = dy = 25 # mouseX, mouseY = -1, -1 # cap = cv2.VideoCapture(0) # # ret, frame = cap.read() # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # cv2.namedWindow('frame') # cv2.imshow('frame', gray) # cv2.setMouseCallback('frame', draw_circle) # while (True): # # Capture frame-by-frame # ret, frame = cap.read() # # # Our operations on the frame come here # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # height, width = gray.shape # # pix_to_deg_v = height / fov_v # pix_to_deg_h = width / fov_h # # # Display the resulting frame # cv2.line(gray, (width / 4, height / 4 - 10), (width / 4, height / 4 + 10), (0, 255, 0), 3) # cv2.line(gray, (width / 4 - 10, height / 4), (width / 4 + 10, height / 4), (0, 255, 0), 3) # # cv2.line(gray, (3 * width / 4, 3 * height / 4 - 10), (3 * width / 4, 3 * height / 4 + 10), (0, 255, 0), 3) # cv2.line(gray, (3 * width / 4 - 10, 3 * height / 4), (3 * width / 4 + 10, 3 * height / 4), (0, 255, 0), 3) # # cv2.line(gray, (width / 4, 3 * height / 4 - 10), (width / 4, 3 * height / 4 + 10), (0, 255, 0), 3) # cv2.line(gray, (width / 4 - 10, 3 * height / 4), (width / 4 + 10, 3 * height / 4), (0, 255, 0), 3) # # cv2.line(gray, (3 * width / 4, height / 4 - 10), (3 * width / 4, height / 4 + 10), (0, 255, 0), 3) # cv2.line(gray, (3 * width / 4 - 10, height / 4), (3 * width / 4 + 10, height / 4), (0, 255, 0), 3) # # if mouseX > -1 and mouseY > -1: # cv2.circle(gray, (mouseX, mouseY), 10, (0, 0, 0), thickness=3, lineType=8, shift=0) # # cv2.circle(gray, (width / 2, height / 2), 10, (22, 222, 22), thickness=3, lineType=8, shift=0) # # error_x = width / 2 - mouseX # error_y = height / 2 - mouseY # # error_in_deg_v = error_y / pix_to_deg_v # error_in_deg_h = error_x / pix_to_deg_h # # print (error_in_deg_h, error_in_deg_v) # cv2.imshow('frame', gray) # # # print(cv2.waitKey(1)) # # temp = 0 # lock.acquire() # try: # temp = ra # finally: # lock.release() # # key = cv2.waitKey(1) # if key & 0xFF == ord('q'): # print("breaking") # break # if key & 0xFF == ord('w'): # temp = temp + 5 # print("ra(temp): {}".format(temp)) # if key & 0xFF == ord('s'): # temp = temp - 5 # print("ra(temp): {}".format(temp)) # # lock.acquire() # try: # ra = temp # finally: # lock.release() # # # When everything done, release the capture # cap.release() # cv2.destroyAllWindows() # otherwise, for OpenCV 3.3 OR NEWER, we need to explicity call the # initialize a dictionary that maps strings to their corresponding # OpenCV object tracker implementations OPENCV_OBJECT_TRACKERS = { # "csrt": cv2.TrackerCSRT_create, "kcf": cv2.TrackerKCF_create, "boosting": cv2.TrackerBoosting_create, "mil": cv2.TrackerMIL_create, "tld": cv2.TrackerTLD_create, "medianflow": cv2.TrackerMedianFlow_create, # "mosse": cv2.TrackerMOSSE_create } # grab the appropriate object tracker using our dictionary of # OpenCV object tracker objects # tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]() tracker = OPENCV_OBJECT_TRACKERS['medianflow']() # initialize the bounding box coordinates of the object we are going # to track initBB = None print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(1.0) # initialize the FPS throughput estimator fps = None # loop over frames from the video stream while True: # grab the current frame, then handle if we are using a # VideoStream or VideoCapture object frame = vs.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) height, width = gray.shape # check to see if we have reached the end of the stream if frame is None: break # resize the frame (so we can process it faster) and grab the # frame dimensions # frame = imutils.resize(frame, width=500) (H, W) = frame.shape[:2] # check to see if we are currently tracking an object if initBB is not None: # grab the new bounding box coordinates of the object (success, box) = tracker.update(frame) # check to see if the tracking was a success if success: (x, y, w, h) = [int(v) for v in box] cv2.rectangle(frame, (x, y), (x + dx, y + dy), (0, 255, 0), 2) error_x = width / 2 - x error_y = height / 2 - y error_in_deg_v = error_y / pix_to_deg_v error_in_deg_h = error_x / pix_to_deg_h # print (error_in_deg_h, error_in_deg_v) # update the FPS counter fps.update() fps.stop() # initialize the set of information we'll be displaying on # the frame info = [ ("Tracker", 'medianflow'), ("Success", "Yes" if success else "No"), ("FPS", "{:.2f}".format(fps.fps())), ] # loop over the info tuples and draw them on our frame for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # check to see if we are currently tracking an object pix_to_deg_v = height / fov_v pix_to_deg_h = width / fov_h # Display the resulting frame cv2.line(gray, (width / 4, height / 4 - 10), (width / 4, height / 4 + 10), (0, 255, 0), 3) cv2.line(gray, (width / 4 - 10, height / 4), (width / 4 + 10, height / 4), (0, 255, 0), 3) cv2.line(gray, (3 * width / 4, 3 * height / 4 - 10), (3 * width / 4, 3 * height / 4 + 10), (0, 255, 0), 3) cv2.line(gray, (3 * width / 4 - 10, 3 * height / 4), (3 * width / 4 + 10, 3 * height / 4), (0, 255, 0), 3) cv2.line(gray, (width / 4, 3 * height / 4 - 10), (width / 4, 3 * height / 4 + 10), (0, 255, 0), 3) cv2.line(gray, (width / 4 - 10, 3 * height / 4), (width / 4 + 10, 3 * height / 4), (0, 255, 0), 3) cv2.line(gray, (3 * width / 4, height / 4 - 10), (3 * width / 4, height / 4 + 10), (0, 255, 0), 3) cv2.line(gray, (3 * width / 4 - 10, height / 4), (3 * width / 4 + 10, height / 4), (0, 255, 0), 3) if update_tracker and mouseX > -1 and mouseY > -1: update_tracker = False #frame = vs.read() cv2.circle(frame, (mouseX, mouseY), 10, (0, 0, 0), thickness=3, lineType=8, shift=0) cv2.rectangle(frame, (mouseX - dx, mouseY - dy), (mouseX + dx, mouseY + dy), (0, 0, 255), 2) initBB = (mouseX - dx, mouseY - dy, mouseX + dx, mouseY + dy) # print (initBB) tracker = OPENCV_OBJECT_TRACKERS['medianflow']() tracker.init(frame, initBB) fps = FPS().start() cv2.circle(frame, (width / 2, height / 2), 10, (22, 222, 22), thickness=3, lineType=8, shift=0) # error_x = width / 2 - mouseX # error_y = height / 2 - mouseY # error_in_deg_v = error_y / pix_to_deg_v # error_in_deg_h = error_x / pix_to_deg_h # print (error_in_deg_h, error_in_deg_v) # show the output frame cv2.imshow("Frame", frame) cv2.setMouseCallback("Frame", draw_circle) key = cv2.waitKey(1) & 0xFF # if the 's' key is selected, we are going to "select" a bounding # box to track if key == ord("s"): # select the bounding box of the object we want to track (make # sure you press ENTER or SPACE after selecting the ROI) initBB = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) # print (initBB) # start OpenCV object tracker using the supplied bounding box # coordinates, then start the FPS throughput estimator as well tracker.init(frame, initBB) fps = FPS().start() # if the `q` key was pressed, break from the loop elif key == ord("q"): break # if we are using a webcam, release the pointer vs.stop() # # otherwise, release the file pointer # else: # vs.release() # close all windows cv2.destroyAllWindows() killFlag = True
# initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # start the video stream thread print("[INFO] starting video stream thread...") # vs = FileVideoStream(args["video"]).start() # fileStream = True vs = VideoStream(src=0).start() # vs = VideoStream(usePiCamera=True).start() fileStream = False time.sleep(1.0) # loop over frames from the video stream while True: # if this is a file video stream, then we need to check if # there any more frames left in the buffer to process if fileStream and not vs.more(): break # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale # channels) frame = vs.read()
EYE_AR_THRESH = 0.20 EYE_AR_CONSEC_FRAMES = 48 COUNTER = 0 ALARM_ON = False print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(s) (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] print("[INFO] starting video stream thread...") vs = VideoStream(src=w).start() time.sleep(1.0) while True: frame = vs.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = detector(gray, 0) for rect in rects: shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd]
######################## #counter = 1 ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help="path to the video file") ap.add_argument("-a", "--min-area", type=int, default=250, help="minimum area size") args = vars(ap.parse_args()) if args.get("video", None) is None: vs = VideoStream(src=0).start() time.sleep(0.5) else: vs = cv2.VideoCapture(args["video"]) firstFrame = None fgbg = cv2.createBackgroundSubtractorMOG2() # loop over the frames of the video while True: frame = vs.read() frame = frame if args.get("video", None) is None else frame[1] text = "Not Detected" txt2 = "Car" if frame is None:
# --shape-predictor : The path to dlib’s pre-trained facial landmark detector. # Use the “Downloads” section of this blog post to download an archive of the # code + facial landmark predictor file. # --picamera : An optional command line argument, this switch indicates # whether the Raspberry Pi camera module should be used instead of the default # webcam/USB camera. Supply a value > 0 to use your Raspberry Pi camera. # initialize dlib's face detector (HOG-based) and then create the facial # landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) # initialize the video stream and allow the camera sensor to warm-up print("[INFO] camera sensor warming up...") vs = VideoStream(usePiCamera=args["picamera"] > 0).start() time.sleep(2.0) # the heart of out video processing pipeline can be found inside the while loop # below # loop over the frames from the video stream while True: # grab the frame from the threaded video stream, resize it to # have a maximum width of 400 pixels, and convert it to # grayscale frame = vs.read() frame = imutils.resize(frame, width=500) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0)
"--video", required=True, help="path to input video file") args = vars(ap.parse_args()) # initialize dlib's face detector (HOG-based) and then create the # facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) # initialize the video stream and sleep for a bit, allowing the # camera sensor to warm up print("[INFO] camera sensor warming up...") vs = VideoStream(src=str(args["video"]), framerate=30).start() # vs = VideoStream(usePiCamera=True).start() # Raspberry Pi time.sleep(2.0) # 400x225 to 1024x576 frame_width = 1024 frame_height = 576 # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file. out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 30, (frame_width, frame_height)) # loop over the frames from the video stream # 2D image points. If you change the image, you need to change vector image_points = np.array( [
ser = serial.Serial(DEVICE, BAUD) ap=argparse.ArgumentParser() ap.add_argument("-p", "--picamera", type=int, default=-1, help="whether or not the Raspberry Pi camera should be used") ap.add_argument("-v", "--video", help="path to video file") ap.add_argument("-b", "--buffer", type=int, default=64, help="max buffer size") args=vars(ap.parse_args()) red_lower = (1, 1, 1) red_upper = (255, 240, 240) green_lower = (40, 50, 25) green_upper = (80, 255, 150) pts = deque(maxlen=args["buffer"]) camera = VideoStream(usePiCamera=args["picamera"] > 0).start() #go to target theta_total="1/011/2/020/3/155/4/080" print theta_total print 'going to target' ser.write(theta_total) #time.sleep(2) ser.write(theta_total) #time.sleep(8) time.sleep(2.0) while(1): (frame)=camera.read() frame=imutils.resize(frame, width=400) hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask=cv2.inRange(hsv, green_lower, green_upper)
def start(): #render_template('1.html') EYE_AR_THRESH = 0.25 max = 0.3 EYE_AR_CONSEC_FRAMES = 30 # initialize the frame counter as well as a boolean used to # indicate if the alarm is going off COUNTER = 0 ALARM_ON = False # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # start the video stream thread print("[INFO] starting video stream thread...") vs = VideoStream(0).start() time.sleep(1.0) # loop over frames from the video stream while True: # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale # channels) frame = vs.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) # loop over the face detections for rect in rects: # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 if ear > max: max = ear EYE_AR_THRESH = max * 0.75 print(EYE_AR_THRESH) # compute the convex hull for the left and right eye, then # visualize each of the eyes leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # check to see if the eye aspect ratio is below the blink # threshold, and if so, increment the blink frame counter if ear < EYE_AR_THRESH: COUNTER += 1 # if the eyes were closed for a sufficient number of # then sound the alarm if COUNTER >= EYE_AR_CONSEC_FRAMES: # if the alarm is not on, turn it on if not ALARM_ON: ALARM_ON = True # check to see if an alarm file was supplied, # and if so, start a thread to have the alarm # sound played in the background if ('alarm.wav') != "": t = Thread(target=sound_alarm('alarm.wav'), args=('alarm.wav')) t.deamon = True t.start() # draw an alarm on the frame cv2.putText(frame, "DROWSINESS ALERT!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # otherwise, the eye aspect ratio is not below the blink # threshold, so reset the counter and alarm else: COUNTER = 0 ALARM_ON = False # draw the computed eye aspect ratio on the frame to help # with debugging and setting the correct eye aspect ratio # thresholds and frame counters cv2.putText(frame, "EAR: {:.2f}".format(ear), (300, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # show the frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # do a bit of cleanup cv2.destroyAllWindows() vs.stop() return render_template("1.html")
class TEVideoHandler: def __init__(self): self.FRAME_WIDTH = conf.DEF_FRAME_WIDTH self.FRAME_HEIGHT = conf.DEF_FRAME_HEIGHT # devices self.video_file = None self.camera = None self.picamera = None def set_frame_size(w, h): if self.video_file is not None or self.camera is not None or self.picamera is not None: raise TEVideoException("Frame size need to be set before initialization") self.FRAME_WIDTH = w self.FRAME_HEIGHT = h def initialize_with_file(self, filename): if self.video_file is not None or self.camera is not None or self.picamera is not None: raise TEVideoException("Already Initialized") self.video_file = cv2.VideoCapture(filename) def initialize_with_configured_cam(self): cam_selector = { conf.CameraType.PYCAMERA: lambda: self.initialize_with_pycamera(), conf.CameraType.PYCAMERA_ROBUST: lambda: self.initialize_with_pycamera2(), conf.CameraType.WEBCAM: lambda: self.initialize_with_webcam(), } cam_selector[conf.CAMERA_TYPE]() def initialize_with_pycamera(self): if self.video_file is not None or self.camera is not None or self.picamera is not None: raise TEVideoException("Already Initialized") self.camera = VideoStream(usePiCamera=True).start() time.sleep(2.0) # It uses picamera library to disable auto control feature def initialize_with_pycamera2(self): if self.video_file is not None or self.camera is not None or self.picamera is not None: raise TEVideoException("Already Initialized") self.picamera = PiCamera() self.picamera.resolution = (self.FRAME_WIDTH, self.FRAME_HEIGHT) self.picamera.framerate = 30 self.rawCapture = PiRGBArray(self.picamera, size=(self.FRAME_WIDTH, self.FRAME_HEIGHT)) time.sleep(0.1) self.picamera.shutter_speed = self.picamera.exposure_speed self.picamera.exposure_mode = 'off' g = self.picamera.awb_gains self.picamera.awb_mode = 'off' self.picamera.awb_gains = g self.stream = self.picamera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) # Tested with a monitor webcam, but didn't checked with the webcam macbook def initialize_with_webcam(self): if self.video_file is not None or self.camera is not None or self.picamera is not None: raise TEVideoException("Already Initialized") self.camera = VideoStream().start() time.sleep(2.0) # Read a frame # Return: frame (pixel array) # Note: if not grapped (for video file), raise exception def read(self): frame = None if self.video_file is not None: (grabbed, frame) = self.video_file.read() if not grabbed: raise TEInvalidFrameException() elif self.camera is not None: frame = self.camera.read() elif self.picamera is not None: data = self.stream.next() frame = data.array self.rawCapture.truncate(0) # If still null frame, if frame is None: raise TEInvalidFrameException() # resize the frame frame = imutils.resize(frame, width=self.FRAME_WIDTH) return frame def release(self): if self.video_file is not None: self.video_file.release() elif self.camera is not None: # Pycamera may not have the release function if hasattr(self.camera, 'release'): self.camera.release()
# second can be used to derive the bounding box coordinates of text # load the pre-trained EAST text detector print("[INFO] loading EAST text detector...") model_xml = args["east"] model_bin = os.path.splitext(model_xml)[0] + ".bin" ie = IECore() net = ie.read_network(model_xml, model_bin) input_info = net.input_info input_blob = next(iter(input_info)) exec_net = ie.load_network(network=net, device_name=args["device"]) # if a video path was not supplied, grab the reference to the web cam if not args.get("video", False): print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(1.0) # otherwise, grab a reference to the video file else: vs = cv2.VideoCapture(args["video"]) # start the FPS throughput estimator fps = FPS().start() # loop over frames from the video stream while True: t1 = time.perf_counter() # grab the current frame, then handle if we are using a # VideoStream or VideoCapture object
ap.add_argument("-c", "--confidence", type = float, default = 0.4, help = "minimum probability to filter weak detections") ap.add_argument("-s", "--skip-frames", type = int, default = 30, help = "# of skip frames between detections") args = vars(ap.parse_args()) # initialize the list of class labels CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] # load our serialized model from the disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # if a video path was not supplied, grab a reference to the webcam if not args.get("input", False): print("[INFO] Starting video stream...") vs = VideoStream(src = 0).start() time.sleep(2.0) # otherwise, grab a reference to the video file else: print("[INFO] opening the video file...") vs = cv2.VideoCapture(args["input"]) # initialize the video writer writer = None # initialize the frame dimensions W = None H = None
def move_mouse_to(x, y): extra = ctypes.c_ulong(0) ii_ = Input_I() ii_.mi = MouseInput(x, y, 0, 0x0001, 0, ctypes.pointer(extra)) command = Input(ctypes.c_ulong(0), ii_) ctypes.windll.user32.SendInput(1, ctypes.pointer(command), ctypes.sizeof(command)) ########################### # Tracking ########################### # Open video stream vs = VideoStream(src=videoSource + cv2.CAP_DSHOW).start() if applyFullHdFix: # Enable Full HD vs.stream.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) vs.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) # Enable compression vs.stream.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')) if showCameraSettings: # Open webcam settings window vs.stream.set(cv2.CAP_PROP_SETTINGS, 1) # Allow camera to warm up time.sleep(2.0) # Previous tracked center
class DualCamera(): def __init__(self): self.root = Tk() self.root.wm_title("Dual Cam") self.saving = False self.frames = [] self.overlay = False self.image_left = ImageTk.PhotoImage(image=Image.fromarray(np.uint8(np.zeros((256, 256))))) self.image_panel_left = Label(self.root, image = self.image_left) self.image_panel_left.grid(row = 0, column = 0, columnspan=2) self.image_right = ImageTk.PhotoImage(image=Image.fromarray(np.uint8(256 * np.random.rand(256, 256)))) self.image_panel_right = Label(self.root, image = self.image_right) self.image_panel_right.grid(row = 0, column = 2, columnspan=2) self.save_button = Button(width = 10, height = 2, text = 'Save', command=self.save) self.save_button.grid(row = 1, column = 0) self.calibrate_button = Button(width = 10, height = 2, text = 'Calibrate', command=self.calibrate) self.calibrate_button.grid(row = 1, column = 1) self.close_button = Button(width = 10, height = 2, text = 'Close', command=self.quit) self.close_button.grid(row = 1, column = 3) self.overlay_button = Button(width=10, height=2, text='Overlay', command=self.toggle_overlay) self.overlay_button.grid(row = 1, column = 2) self.bias_slider = Scale(self.root, from_=0, to=31, length=400, orient=HORIZONTAL, command=self.bias) self.bias_slider.grid(row = 2, column = 1, columnspan=3) self.bias_label = Label(self.root, text="Bias current") self.bias_label.grid(row=2, column=0) self.clock_slider = Scale(self.root, from_=0, to=63, length=400, orient=HORIZONTAL, command=self.clock) self.clock_slider.grid(row = 3, column = 1, columnspan=3) self.clock_label = Label(self.root, text="Clock speed") self.clock_label.grid(row=3, column=0) self.cm_slider = Scale(self.root, from_=0, to=31, length=400, orient=HORIZONTAL, command=self.cm) self.cm_slider.grid(row = 4, column = 1, columnspan=3) self.cm_label = Label(self.root, text="CM current") self.cm_label.grid(row=4, column=0) # set default positions self.cm_slider.set(0x0C) self.clock_slider.set(0x15) self.bias_slider.set(0x05) # initialize visible camera self.vs = VideoStream(usePiCamera=True).start() # thread for reading from sensor hardware intro an image queue self.ir_images = Queue.LifoQueue() self.ir_commands = Queue.Queue() self.ir_calibrate = threading.Event() self.ir_stop = threading.Event() self.raw_ir_images = Queue.LifoQueue() self.capture_thread = threading.Thread( target=ir_capture, name="capture_thread", args=[self.ir_images, self.ir_calibrate, self.ir_stop, self.ir_commands, self.raw_ir_images] ) self.capture_thread.start() self.ticktock() self.root.mainloop() def ticktock(self): # grab an image from the camera frame = self.vs.read() changed = False if frame is not None: frame = imutils.resize(frame, height=240) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) self.last_vis_frame = frame image = Image.fromarray(frame) if not self.overlay: self.image_right = ImageTk.PhotoImage(image=image) self.image_panel_right.configure(image=self.image_right); changed = True if not self.ir_images.empty(): ir_frame = self.ir_images.get() if not self.ir_images.empty(): with self.ir_images.mutex: self.ir_images.queue = [] ir_image = imutils.resize(ir_frame, height=240, inter=cv2.INTER_LINEAR) ir_image = np.dstack((ir_image, ir_image, ir_image)) ir_image = cv2.LUT(ir_image, colormap).astype('uint8') self.last_ir_frame = ir_image self.image_left = ImageTk.PhotoImage(image=Image.fromarray(ir_image)) self.image_panel_left.configure(image=self.image_left) changed = True if changed and self.overlay: overlay_image = np.zeros_like(self.last_vis_frame) overlay_image[:,:,2] = 0.125 * self.last_vis_frame[:,:,0] + 0.25 * self.last_vis_frame[:,:,1] + 0.125 * self.last_vis_frame[:,:,2] converted_frame = cv2.cvtColor(self.last_ir_frame, cv2.COLOR_RGB2HSV) overlay_image[:,40:280,2] += 0.5 * converted_frame[:,:,2] overlay_image[:,40:280,1] = converted_frame[:,:,1] overlay_image[:,40:280,0] = converted_frame[:,:,0] overlay_image = cv2.cvtColor(overlay_image, cv2.COLOR_HSV2RGB) self.image_right = ImageTk.PhotoImage(image=Image.fromarray(overlay_image)) self.image_panel_right.configure(image=self.image_right) if self.saving: if not self.raw_ir_images.empty(): ir_frame = self.raw_ir_images.get() if not self.raw_ir_images.empty(): with self.raw_ir_images.mutex: self.raw_ir_images.queue = [] else: ir_frame = None self.frames.append((frame, ir_frame)) if not self.ir_calibrate.isSet(): self.calibrate_button.configure(text = 'Calibrate', command=self.calibrate, state="normal") self.root.after(100, self.ticktock) def quit(self): self.vs.stop() self.ir_stop.set() self.root.quit() def save(self): self.save_button.configure(text = 'Stop Saving', command=self.stop_save) self.saving = True self.frames = [] def stop_save(self): self.save_button.configure(text = 'Save', command=self.save) now = time.strftime("%Y-%m-%dT%H:%M:%S") pickle.dump(self.frames, open(now + ".p", "wb")) self.frames = [] def calibrate(self): self.calibrate_button.configure(text = 'Calibrating...', state="disabled") self.ir_calibrate.set() def cm(self, val): val = int(val) self.ir_commands.put(('cm', val)) def bias(self, val): val = int(val) self.ir_commands.put(('bias', val)) def clock(self, val): val = int(val) self.ir_commands.put(('clock', val)) def toggle_overlay(self): if self.overlay: self.overlay = False self.overlay_button.configure(text = 'Overlay') else: self.overlay = True self.overlay_button.configure(text = 'No Overlay')
def video_processing(graph, category_index, video_file_name, show_video_window, camera_id, run_flag, message_queue): if camera_id is None: cap = cv2.VideoCapture(video_file_name) ending_frame = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) input_fps = cap.get(cv2.CAP_PROP_FPS) ret, frame = cap.read() resized_frame = cv2.resize(frame, dsize=(config.display_window_width, config.display_window_height)) size = (resized_frame.shape[:2]) video_output = 'output.mp4' output_fps = input_fps / 1 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(video_output, fourcc, output_fps, (resized_frame.shape[1], resized_frame.shape[0])) if show_video_window: cv2.namedWindow('ppe', cv2.WINDOW_NORMAL) if config.display_full_screen: cv2.setWindowProperty('ppe', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) else: cv2.setWindowProperty('ppe', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_NORMAL) if (config.capture_image_width, config.capture_image_height ) in config.supported_video_resolution: print("video_processing:", "supported video resolution") cap.set(cv2.CAP_PROP_FRAME_WIDTH, config.capture_image_width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, config.capture_image_height) video_output = "output.mp4" with graph.as_default(): print("video_processing:", "default tensorflow graph") ops = tf.get_default_graph().get_operations() all_tensor_names = { output.name for op in ops for output in op.outputs } tensor_dict = {} for key in [ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks' ]: tensor_name = key + ':0' if tensor_name in all_tensor_names: tensor_dict[key] = tf.get_default_graph( ).get_tensor_by_name(tensor_name) with tf.Session() as sess: print("video_processing:", "tensorflow session") send_message_time = time.time() frame_counter = 0 i = 0 # default is 0 while (cap.isOpened()) and ret is True: ret, frame = cap.read() if config.input_type.lower() == "file": frame_counter += 1 if frame_counter == int( cap.get(cv2.CAP_PROP_FRAME_COUNT)): frame_counter = 0 cap.set(cv2.CAP_PROP_POS_FRAMES, 0) continue if frame is None: print("video_processing:", "null frame") break resized_frame = cv2.resize(frame, dsize=(640, 360)) image_expanded = np.expand_dims(resized_frame, axis=0) output_dict = run_inference_for_single_image( image_expanded, sess, tensor_dict) detection_scores = np.where( output_dict["detection_scores"] > 0.7, True, False) detection_boxes = output_dict["detection_boxes"][ detection_scores] detection_classes = output_dict["detection_classes"][ detection_scores] '''head_boxes = detection_boxes[np.where(detection_classes == 1)] body_boxes = detection_boxes[np.where(detection_classes == 2)]''' person_boxes = detection_boxes[np.where( detection_classes == 1)] persons = [] for person_box in person_boxes: person = dict() #person["head"], person["body"] = is_person(head_boxes, body_boxes, person_box) person["head"], person["body"] = is_person(person_box) persons.append(person) vis_utils.visualize_boxes_and_labels_on_image_array( frame, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'], category_index, instance_masks=output_dict.get('detection_masks'), use_normalized_coordinates=True, line_thickness=4) if time.time( ) - send_message_time > config.message_send_interval / 1000.0: resized_frame = cv2.resize( frame, dsize=(config.storage_image_width, config.storage_image_height)) try: message_queue.put_nowait( (camera_id, output_dict, resized_frame, config.object_confidence_threshold)) except queue.Full: print("message queue is full") else: send_message_time = time.time() if show_video_window: resized_frame = cv2.resize( frame, dsize=(config.display_window_width, config.display_window_height)) height, width = resized_frame.shape[:2] head_count = 0 body_count = 0 head_and_body_count = 0 for person in persons: if person['head'] and person['body']: head_and_body_count += 1 elif person['head']: head_count += 1 elif person['body']: body_count += 1 resized_frame = cv2.putText( resized_frame, "No of person:" + str(len(person_boxes)), (30, height - 170), cv2.FONT_HERSHEY_TRIPLEX, 1, (150, 100, 50), 2, cv2.LINE_AA) cv2.imshow('ppe', resized_frame) out.write(resized_frame) if cv2.waitKey(1) & 0xFF == ord('q'): run_flag.value = 0 break k = cv2.waitKey(30) & 0xff if k == 27: break else: print("[INFO] starting cameras...") cap = VideoStream(src=int(camera_id)).start() # picam = VideoStream(usePiCamera=True).start() # read the next frame from the video stream and resize # it to have a maximum width of 400 pixels frame = cap.read() resized_frame = cv2.resize(frame, dsize=(config.display_window_width, config.display_window_height)) size = (resized_frame.shape[:2]) video_output = 'output.mp4' output_fps = 30 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(video_output, fourcc, output_fps, (resized_frame.shape[1], resized_frame.shape[0])) if show_video_window: cv2.namedWindow('ppe', cv2.WINDOW_NORMAL) if config.display_full_screen: cv2.setWindowProperty('ppe', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) else: cv2.setWindowProperty('ppe', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_NORMAL) with graph.as_default(): print("video_processing:", "default tensorflow graph") ops = tf.get_default_graph().get_operations() all_tensor_names = { output.name for op in ops for output in op.outputs } tensor_dict = {} for key in [ 'num_detections', 'detection_boxes', 'detection_scores', 'detection_classes', 'detection_masks' ]: tensor_name = key + ':0' if tensor_name in all_tensor_names: tensor_dict[key] = tf.get_default_graph( ).get_tensor_by_name(tensor_name) with tf.Session() as sess: print("video_processing:", "tensorflow session") send_message_time = time.time() frame_counter = 0 i = 0 # default is 0 dbImage = Image("Img.db") while True: frame = cap.read() if frame is None: print("video_processing:", "null frame") break frame_counter += 1 resized_frame = cv2.resize(frame, dsize=(640, 360)) image_expanded = np.expand_dims(resized_frame, axis=0) output_dict = run_inference_for_single_image( image_expanded, sess, tensor_dict) detection_scores = np.where( output_dict["detection_scores"] > 0.7, True, False) detection_boxes = output_dict["detection_boxes"][ detection_scores] detection_classes = output_dict["detection_classes"][ detection_scores] '''head_boxes = detection_boxes[np.where(detection_classes == 1)] body_boxes = detection_boxes[np.where(detection_classes == 2)]''' person_boxes = detection_boxes[np.where( detection_classes == 1)] persons = [] for person_box in person_boxes: person = dict() #person["head"], person["body"] = is_person(head_boxes, body_boxes, person_box) person["head"], person["body"] = is_person(person_box) persons.append(person) vis_utils.visualize_boxes_and_labels_on_image_array( frame, output_dict['detection_boxes'], output_dict['detection_classes'], output_dict['detection_scores'], category_index, instance_masks=output_dict.get('detection_masks'), use_normalized_coordinates=True, line_thickness=4) if time.time( ) - send_message_time > config.message_send_interval / 1000.0: resized_frame = cv2.resize( frame, dsize=(config.storage_image_width, config.storage_image_height)) try: message_queue.put_nowait( (camera_id, output_dict, resized_frame, config.object_confidence_threshold)) except queue.Full: print("message queue is full") else: send_message_time = time.time() if show_video_window: resized_frame = cv2.resize( frame, dsize=(config.display_window_width, config.display_window_height)) height, width = resized_frame.shape[:2] head_count = 0 body_count = 0 head_and_body_count = 0 for person in persons: if person['head'] and person['body']: head_and_body_count += 1 elif person['head']: head_count += 1 elif person['body']: body_count += 1 resized_frame = cv2.putText( resized_frame, "No of person: " + str(len(person_boxes)), (30, height - 170), cv2.FONT_HERSHEY_TRIPLEX, 1, (150, 100, 50), 2, cv2.LINE_AA) cv2.imshow('ppe', resized_frame) if len(person_boxes) >= 1: pic_name = "frame" + str(frame_counter) + ".jpg" cv2.imwrite("./Pictures/" + pic_name, resized_frame) with open("./Pictures/" + pic_name, 'rb') as f: dbImage.create_database(name=pic_name, image=f.read()) out.write(resized_frame) if cv2.waitKey(1) & 0xFF == ord('q'): run_flag.value = 0 break print("video_processing:", "releasing video capture") out.release() #cap.release() cv2.destroyAllWindows()
ap.add_argument("-p", "--picamera", type=int, default=-1, help="whether or not the Raspberry Pi camera should be used") ap.add_argument("-f", "--fps", type=int, default=15, help="FPS of output video") ap.add_argument("-c", "--codec", type=str, default="MJPG", #XVID codec works on linux, not on apple. MJPG works on both. help="codec of output video") ap.add_argument("-w", "--width", type=int, default=600, help="width size; height will be determined to keep proper frame ratio") ap.add_argument("-n", "--windowname", type=str, default= windowName, help="name of the window and trackbars") args = vars(ap.parse_args()) #initialize the video stream and allow the camera sensor to warmup print("Warming up camera...") # vs = VideoStream(usePiCamera=args["picamera"] > 0).start() vs = VideoStream(src=0).start() time.sleep(2.0) #initialize the FourCC, video writer, dimensions of the frame, and zeros array fourcc = cv2.VideoWriter_fourcc(*args["codec"]) out = None (h, w) = (None, None) zeros = None A = None wait_time = None record = False WindowName = args["windowname"] n=0 print("Initialize streaming") while True:
def getData(): args = {'image': 'rooster.jpg', 'prototxt': 'deploy.prototxt.txt', 'model': 'res10_300x300_ssd_iter_140000.caffemodel', 'confidence': 0.5} # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # initialize the video stream and allow the cammera sensor to warmup print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) # loop over the frames from the video stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=400) # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) # pass the blob through the network and obtain the detections and # predictions net.setInput(blob) detections = net.forward() # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the # prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence < args["confidence"]: continue # compute the (x, y)-coordinates of the bounding box for the # object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # draw the bounding box of the face along with the associated # probability text = "{:.2f}%".format(confidence * 100) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2) cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
import numpy as np, cv2, datetime, time from imutils.video import VideoStream import imutils import argparse #Argument parser to select picamera or USB webcamera ap=argparse.ArgumentParser() ap.add_argument("-p", "--picamera", type=int, default=-1, help="whether or not the Raspberry Pi camera should be used") ap.add_argument("-v", "--video", help="path to video file") ap.add_argument("-b", "--buffer", type=int, default=64, help="max buffer size") args=vars(ap.parse_args()) camera = VideoStream(usePiCamera=args["picamera"] > 0).start() time.sleep(2) #camera start time while True: #Read the frame frame = camera.read() #Reshape to 400 pixel width frame = imutils.resize(frame, width=400) #Display frame cv2.imshow('OrigFrame',frame) #Press 'q' to quit key=cv2.waitKey(1)& 0xFF if key==ord("q"): break #close the imshow window cv2.destroyAllWindows()
FRAME_WIDTH = 640 FRAME_HEIGHT = 480 # 依不同的 cascade 做調整 # lbpcascade_frontalface: 1.1 # haarcascade_frontalface_alt2: 1.3 SCALE_FACTOR = 1.1 MIN_NEIGHBORS = 5 #MIN_SIZE = 30 MIN_SIZE = 80 cascPath = sys.argv[1] faceCascade = cv2.CascadeClassifier(cascPath) if ENABLE_VIDEO_STREAM: video_capture = VideoStream(usePiCamera=False).start() else: video_capture = cv2.VideoCapture(0) video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, FRAME_WIDTH) video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, FRAME_HEIGHT) time.sleep(1) t = ticket() def faceDetect(gray): faces = faceCascade.detectMultiScale( gray, scaleFactor=SCALE_FACTOR, minNeighbors=MIN_NEIGHBORS, minSize=(MIN_SIZE, MIN_SIZE),
# define the lower and upper boundaries of the "green" # ball in the HSV color space greenLower = (24, 116, 137) greenUpper = (36, 255, 255) # initialize the list of tracked points, the frame counter, # and the coordinate deltas pts = deque(maxlen=args["buffer"]) counter = 0 (dX, dY) = (0, 0) direction = "" # if a video path was not supplied, grab the reference # to the webcam if not args.get("video", False): vs = VideoStream(usePiCamera=1).start() time.sleep(2.0) # otherwise, grab a reference to the video file else: vs = cv2.VideoCapture(args["video"]) # keep looping while True: # grab the current frame if args.get("video"): (grabbed, frame) = vs.read() else: frame = vs.read() # if we are viewing a video and we did not grab a frame,
import warnings import json import cv2 from tempimage import TempImage # Parse arguments from JSON config file ap = argparse.ArgumentParser() ap.add_argument("-c", "--conf", required=True, help="path to the JSON configuration file") args = vars(ap.parse_args()) warnings.filterwarnings("ignore") conf = json.load(open(args["conf"])) # initialize the video stream and allow the cammera sensor to warmup #vs = VideoStream(usePiCamera=args["picamera"] > 0).start() vs = VideoStream(usePiCamera=True, framerate=conf["fps"], resolution=tuple(conf["resolution"])).start() time.sleep(conf["camera_warmup_time"]) avg = None lastUploaded = datetime.datetime.now() motionCounter = 0 # loop over the frames from the video stream while True: motionDetected = False # grab the frame from the threaded video stream and resize it # resize the frame, convert it to grayscale, and blur it frame = vs.read() analysisFrame = imutils.resize(frame, width=conf["opencv_image_width"]) gray = cv2.cvtColor(analysisFrame, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (21, 21), 0)
def main(): # construction des arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--prototxt", required=False, default="/home/pi/Kenobi/recognition/MobileNetSSD_deploy.prototxt.txt", help="path to Caffe 'deploy' prototxt file") ap.add_argument("-m", "--model", required=False, default="/home/pi/Kenobi/recognition/MobileNetSSD_deploy.caffemodel", help="path to Caffe pre-trained model") ap.add_argument("-c", "--confidence", type=float, default=0.6, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) # initialiser la liste des objets entrainés par MobileNet SSD # création du contour de détection avec une couleur attribuée au hasard pour chaque objet CLASSES = ["arriere-plan", "avion", "velo", "oiseau", "bateau", "bouteille", "autobus", "voiture", "chat", "chaise", "vache", "table", "chien", "cheval", "moto", "personne", "plante", "mouton", "sofa", "train", "moniteur"] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) pygame.mixer.init() # chargement des fichiers depuis le répertoire de stockage print(" ...chargement du modèle...") net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # initialiser la caméra du pi, attendre 2s pour la mise au point , # initialiser le compteur FPS print("...démarrage de la Picamera...") vs = VideoStream(usePiCamera=True, resolution=(1600, 1200)).start() time.sleep(2.0) #fps = FPS().start() # boucle principale du flux vidéo while True: # récupération du flux vidéo, redimension # afin d'afficher au maximum 800 pixels frame = vs.read() frame = imutils.resize(frame, width=800) # récupération des dimensions et transformation en collection d'images (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) # determiner la détection et la prédiction net.setInput(blob) detections = net.forward() # boucle de détection list_objects = [] for i in np.arange(0, detections.shape[2]): # calcul de la probabilité de l'objet détecté en fonction de la prédiction confidence = detections[0, 0, i, 2] # supprimer les détections faibles inférieures à la probabilité minimale if confidence > args["confidence"]: # extraire l'index du type d'objet détecté # calcul des coordonnées de la fenêtre de détection idx = int(detections[0, 0, i, 1]) #box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) #(startX, startY, endX, endY) = box.astype("int") # creation du contour autour de l'objet détecté # insertion de la prédiction de l'objet détecté #label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100) #cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) #y = startY - 15 if startY - 15 > 15 else startY + 15 #cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) # enregistrement de l'image détectée #cv2.imwrite("detection.png", frame) obj = CLASSES[idx] if obj not in list_objects: list_objects.append(CLASSES[idx]) # affichage du flux vidéo dans une fenètre #cv2.imshow("Frame", frame) #key = cv2.waitKey(1) & 0xFF # ligne necessaire pour l'affichage dans la frame # Pronounce the objects seen print(list_objects) for anobject in list_objects: path_to_sound = "/home/pi/Kenobi/recognition/vocabulary/" + anobject + ".ogg" if os.path.isfile(path_to_sound): pygame.mixer.music.load(path_to_sound) pygame.mixer.music.play() # Play until end of music file while pygame.mixer.music.get_busy() == True: pygame.time.Clock().tick(10) # la touche q permet d'interrompre la boucle principale #if key == ord("q"): # break # mise à jour du FPS #fps.update() # arret du compteur et affichage des informations dans la console #fps.stop() #print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) #print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vs.stop()
import cv2 import sys import time from imutils.video import VideoStream import imutils #setting up the PiCamera and it's variables video_stream = VideoStream(usePiCamera=True, resolution=(480,320), framerate=25).start() time.sleep(2) x = 0 # capture frames from the camera using the piCamera library while True: print(x) # Capture frame-by-frame image = video_stream.read() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Frame", gray) cv2.imwrite("test-images\frame{}.jpg".format(x), gray) key = cv2.waitKey(1) & 0xFF x = x + 1 #break code if key == cv2.waitKey(1) & 0xFF == ord('q'): break
for r in rList: if r.port == 8060: roku = r if roku == 0: # This terminates the program -- I handle upkeep in systemd to save resources raise Exception('No valid Rokus found on network') lastSeen = time.time() try: # Loading model, I DID NOT MAKE THIS, FROM OPENCV GITHUB REPOSITORY net = cv2.dnn.readNetFromCaffe("deploy.prototxt.txt", "model.caffemodel") # initializing pi camera to a video stream, using a python image/video library imutils vs = VideoStream(usePiCamera=True).start() # camera takes some time to turn on, so giving it time here time.sleep(2.0) # loop over the frames from the video stream while True: numFaces = 0 # getting frame frame = vs.read() # grab the frame dimensions and convert it to a blob (Binary Large OBject, pixel grouping detection) (h, w) = frame.shape[:2] # parameters for blob detection taken from OpenCV's facial detection benchmarking program (see OpenCV's Github) blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
class App(object): def __init__(self, video_src): #self.cam = cv2.VideoCapture(video_src) self.cam = VideoStream(usePiCamera=True).start() #self.cam.start() self.frame = self.cam.read() # set channel range of skin detection self.mask_lower_yrb = np.array([41, 142, 109]) # [54, 131, 110] self.mask_upper_yrb = np.array([250, 174, 147]) # [163, 157, 135] # create trackbar for skin calibration self.calib_switch = False # create background subtractor self.fgbg = cv2.createBackgroundSubtractorMOG2(history=10) # define dynamic ROI area self.ROIx, self.ROIy = 0, 0 #self.frame.shape[1]/2, self.frame.shape[0]/2 self.track_switch = True # record previous positions of the centroid of ROI self.preCX = None self.preCY = None # A queue to record last couple gesture command self.last_cmds = FixedQueue() # switch to turn on mouse input control self.cmd_switch = False # count loop (frame), for debugging self.n_frame = 0 # On-line Calibration for skin detection (bug, not stable) def skin_calib(self, raw_yrb): mask_skin = cv2.inRange(raw_yrb, self.mask_lower_yrb, self.mask_upper_yrb) cal_skin = cv2.bitwise_and(raw_yrb, raw_yrb, mask=mask_skin) ymin = cv2.getTrackbarPos('Ymin', 'YRB_calib') ymax = cv2.getTrackbarPos('Ymax', 'YRB_calib') rmin = cv2.getTrackbarPos('CRmin', 'YRB_calib') rmax = cv2.getTrackbarPos('CRmax', 'YRB_calib') bmin = cv2.getTrackbarPos('CBmin', 'YRB_calib') bmax = cv2.getTrackbarPos('CBmax', 'YRB_calib') self.mask_lower_yrb = np.array([ymin, rmin, bmin]) self.mask_upper_yrb = np.array([ymax, rmax, bmax]) # Do skin detection with some filtering def skin_detect(self, raw_yrb, img_src): # use median blurring to remove signal noise in YCRCB domain raw_yrb = cv2.medianBlur(raw_yrb, 5) mask_skin = cv2.inRange(raw_yrb, self.mask_lower_yrb, self.mask_upper_yrb) # morphological transform to remove unwanted part kernel = np.ones((8, 8), np.uint8) res_skin = cv2.bitwise_and(img_src, img_src, mask=mask_skin) #res_skin_dn = cv2.fastNlMeansDenoisingColored(res_skin, None, 10, 10, 7,21) return res_skin # Update Position of ROI def update_ROI(self, img_src): Rxmin, Rymin = 0, 0 Rxmax, Rymax = img_src.shape[1] - 1, img_src.shape[0] - 1 return Rxmin, Rymin, Rxmax, Rymax # Find contour and track hand inside ROI def find_contour(self, img_src, Rxmin, Rymin, Rxmax, Rymax): cv2.rectangle(img_src, (Rxmax, Rymax), (Rxmin, Rymin), (0, 255, 0), 0) crop_res = img_src #[Rymin: Rymax, Rxmin:Rxmax] grey = cv2.cvtColor(crop_res, cv2.COLOR_BGR2GRAY) _, thresh1 = cv2.threshold(grey, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #cv2.imshow('Thresh', thresh1) _, contours, hierchy = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) # draw contour on threshold image if len(contours) > 0: cv2.drawContours(thresh1, contours, -1, (0, 255, 0), 3) return contours, crop_res # Check ConvexHull and Convexity Defects def get_defects(self, cnt, drawing): defects = None hull = cv2.convexHull(cnt) cv2.drawContours(drawing, [cnt], 0, (0, 255, 0), 0) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0) hull = cv2.convexHull(cnt, returnPoints=False) # For finding defects if hull.size > 2: defects = cv2.convexityDefects(cnt, hull) return defects # Gesture Recognition def gesture_recognize(self, cnt, defects, count_defects, crop_res): # use angle between start, end, defect to recognize # of finger show up angles = [] if defects is not None and cv2.contourArea(cnt) >= 1800: for i in range(defects.shape[0]): s, e, f, d = defects[i, 0] start = tuple(cnt[s][0]) end = tuple(cnt[e][0]) far = tuple(cnt[f][0]) a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) angle = math.acos( (b**2 + c**2 - a**2) / (2 * b * c)) * 180 / math.pi if start[1] == len(crop_res) - 2 and end[1] == len( crop_res) - 2: break if angle <= 90: angles.append(angle) count_defects += 1 cv2.circle(crop_res, start, 5, [255, 255, 0], -1) cv2.circle(crop_res, end, 5, [255, 255, 0], -1) cv2.circle(crop_res, far, 5, [0, 0, 255], -1) cv2.line(crop_res, start, end, [0, 255, 0], 2) gesture = 'gesture' if count_defects == 1: if angles[0] > 35: gesture = 'seven' else: gesture = 'two' elif count_defects == 2: gesture = 'three' elif count_defects == 3: gesture = 'four' elif count_defects == 4: gesture = 'five' elif count_defects == 5: if cv2.contourArea(cnt) > 20000: gesture = 'one' else: gesture = 'one' # return the result of gesture recognition return count_defects, gesture # Use PyAutoGUI to control mouse event def input_control(self, count_defects, img_src, gesture): # update position difference with previous frame (for move mouse) d_x, d_y = 0, 0 if self.preCX is not None: d_x = self.ROIx - self.preCX d_y = self.ROIy - self.preCY # checking current command, and filter out unstable hand gesture cur_cmd = 0 if self.cmd_switch: if self.last_cmds.count(count_defects) >= self.last_cmds.n_maj: cur_cmd = count_defects #print 'major command is ', cur_cmd else: cur_cmd = 0 # self.last_cmds.major() else: cur_cmd = count_defects # main function of the project (run all processes) def run(self): while True: #while self.cam.isOpened(): if self.n_frame == 0: ini_time = time.time() #ret, self.frame = self.cam.read() self.frame = self.cam.read() if self.frame is None: time.sleep(0.1) continue self.frame = cv2.flip(self.frame, 1) org_vis = self.frame.copy() #org_vis = cv2.fastNlMeansDenoisingColored(self.frame, None, 10,10,7,21) # try to denoise but time comsuming ### Skin detect filter yrb = cv2.cvtColor(self.frame, cv2.COLOR_BGR2YCR_CB) res_skin = self.skin_detect(yrb, org_vis) ## check if want to do skin calibration if self.calib_switch: self.skin_calib(yrb) org_fg = res_skin ### Find Contours and track hand inside ROI Rxmin, Rymin, Rxmax, Rymax = self.update_ROI(org_fg) contours, crop_res = self.find_contour(org_fg, Rxmin, Rymin, Rxmax, Rymax) ### Get Convexity Defects if Contour in ROI is bigger enough drawing = np.zeros(crop_res.shape, np.uint8) min_area = 1000 max_area = 8500 cnts = [] areas = [] gestures = [] Ms = [] if len(contours) > 0: for i in range(len(contours)): cnt = contours[i] area = cv2.contourArea(cnt) areas.append(area) if area > min_area and area < max_area: cnts.append(cnt) for cnt in cnts: # use minimum rectangle to crop contour for faster gesture checking x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(crop_res, (x, y), (x + w, y + h), (0, 0, 255), 0) # debug draw a circle at center M = cv2.moments(cnt) if M['m00'] != 0: cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) cv2.circle(org_fg, (cx + Rxmin, cy + Rymin), 10, [0, 255, 255], -1) ### Check ConvexHull and Convexity Defects defects = self.get_defects(cnt, drawing) ### Gesture Recognition count_defects = 0 count_defects, gesture = self.gesture_recognize( cnt, defects, count_defects, crop_res) ### Input Control (Mouse Event) self.input_control(count_defects, org_fg, gesture) gestures.append(gesture) Ms.append((cx + Rxmin, cy + Rymin)) # update center position of ROI for next frame self.preCX = self.ROIx self.preCY = self.ROIy print(gestures) print(Ms) mesg = "" if len(Ms) == 1: mesg = str(gestures[0]) x = Ms[0][0] y = Ms[0][1] c = math.sqrt((x - 160)**2 + (y - 120)**2) theta = math.atan2(y - 120, x - 160) * 180 / math.pi if c < 20: mesg = mesg + "_click" else: mesg = mesg + "_rot " + str(theta) elif len(Ms) == 2: m1x = Ms[0][0] m2x = Ms[1][0] m1y = Ms[0][1] m2y = Ms[1][1] if m1x < m2x: mesg = mesg + str(gestures[1]) if m2y < 100: mesg = mesg + "_up" else: mesg = mesg + "_down" mesg = mesg + "," mesg = mesg + str(gestures[0]) if m1y < 100: mesg = mesg + "_up" else: mesg = mesg + "_down" else: mesg = mesg + str(gestures[0]) if m1y < 100: mesg = mesg + "_up" else: mesg = mesg + "_down" mesg = mesg + "," mesg = mesg + str(gestures[1]) if m2y < 100: mesg = mesg + "_up" else: mesg = mesg + "_down" else: mesg = "gesture" client_socket.send(mesg.encode()) ch = cv2.waitKey(5) & 0xFF if ch == 27: break elif ch == ord('c'): self.cmd_switch = not self.cmd_switch elif ch == ord('s'): self.calib_switch = not self.calib_switch elif ch == ord('t'): self.track_switch = not self.track_switch cv2.destroyAllWindows()
greenUpper = (163/2, 100/100*255, 100/100*255) # then initialize the list of tracked points pts = deque(maxlen=args["buffer"]) # to record a video record_video = args.get("record_video", False) record_track = args.get("record_track", False) video_writer = None # if a video path was not supplied, grab the reference # to the picamera from_vfile = args.get("video", False) if not from_vfile: camera = VideoStream(usePiCamera=True).start() time.sleep(2.0) # otherwise, grab a reference to the video file else: video_file = cv2.VideoCapture(args["video"]) # keep looping while True: # grab the current frame if from_vfile: (grabbed, frame) = video_file.read() # if we are viewing a video and we did not grab a frame, # then we have reached the end of the video if not grabbed: break
import numpy as np import imutils import argparse # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-b", "--buffer", type=int, default=64, help="max buffer size") args = vars(ap.parse_args()) # define the lower and upper boundaries of the "red" lower = (0, 100, 80) upper = (10, 255, 255) pts = deque(maxlen=args["buffer"]) # get webcam camera = VideoStream(src=0).start() # keep looping while True: # get the current frame frame = camera.read() cv2.imshow('original frame', frame) # blur and convert to HSV blurred = cv2.GaussianBlur(frame, (11, 11), 0) hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV) # construct a mask for colour, perform erosion & dilation mask = cv2.inRange(hsv, lower, upper) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) cv2.imshow('mask', mask) # perform Hough Transform
giroder=0 stop=0 i=0 template2=cv2.imread("arrowR.jpg") template2 = cv2.cvtColor(template2, cv2.COLOR_BGR2GRAY) template2 = cv2.GaussianBlur(template2, (9, 9), 0) template2 = auto_canny(template2) template3=cv2.imread("arrowStop.jpg") template3 = cv2.cvtColor(template3, cv2.COLOR_BGR2GRAY) template3 = cv2.GaussianBlur(template3, (9, 9), 0) template3 = auto_canny(template3) print("[INFO] waiting for camera to warmup...") vs = VideoStream(0).start() time.sleep(2.0) while True: # grab the next frame from the video stream, resize the # frame, and convert it to the HSV color space frame = cv2.imread("imagen.jpg") frame = imutils.resize(frame, width=500) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #gaussian = GaussianBlur(greyImg, Size(9, 9), 2, 2); blurred = cv2.GaussianBlur(gray, (9, 9), 0) #canny = cv2.Canny(blurred, 10, 200) canny = auto_canny(blurred) (_, cnts , _) = cv2.findContours(canny.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
def main(): # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # grab the indexes of the facial landmarks for the left and # right eye, respectively (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # start the video stream thread vs = VideoStream(src=0).start() fileStream = False # loop over frames from the video stream while True: # if this is a file video stream, then we need to check if # there any more frames left in the buffer to process if fileStream and not vs.more(): break # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale # channels) frame = vs.read() frame = imutils.resize(frame, width=450) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) # loop over the face detections for rect in rects: # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes rightEye = shape[rStart:rEnd] # compute the convex hull for the left and right eye, then # visualize each of the eyes rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # show the frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
ap = argparse.ArgumentParser() ap.add_argument("-o", "--output", required=True, help="path to output video file") ap.add_argument("-p", "--picamera", type=int, default=-1, help="whether or not the Raspberry Pi camera should be used") ap.add_argument("-f", "--fps", type=int, default=20, help="FPS of output video") ap.add_argument("-c", "--codec", type=str, default="MJPG", help="codec of output video") args = vars(ap.parse_args()) # initialize the video stream and allow the camera # sensor to warmup print("[INFO] warming up camera...") vs = VideoStream(usePiCamera=args["picamera"] > 0).start() time.sleep(2.0) # initialize the FourCC, video writer, dimensions of the frame, and # zeros array fourcc = cv2.cv.FOURCC(*args["codec"]) writer = None (h, w) = (None, None) zeros = None # loop over frames from the video stream while True: # grab the frame from the video stream and resize it to have a # maximum width of 300 pixels frame = vs.read() frame = imutils.resize(frame, width=300)
import time import cv2 # initialize the output frame and a lock used to ensure thread-safe # exchanges of the output frames (useful for multiple browsers/tabs # are viewing tthe stream) outputFrame = None lock = threading.Lock() # initialize a flask object app = Flask(__name__) # initialize the video stream and allow the camera sensor to # warmup #vs = VideoStream(usePiCamera=1).start() vs = VideoStream(src=0).start() time.sleep(2.0) @app.route("/") def index(): # return the rendered template return render_template("index.html") def detect_motion(frameCount): # grab global references to the video stream, output frame, and # lock variables global vs, outputFrame, lock # loop over frames from the video stream while True: # read the next frame from the video stream, resize it,