def calculate_fps(self, frames_no=100): fps = FPS().start() # Don't wanna display window if self.debug: self.debug = not self.debug for i in range(0, frames_no): self.where_lane_be() fps.update() fps.stop() # Don't wanna display window if not self.debug: self.debug = not self.debug print('Time taken: {:.2f}'.format(fps.elapsed())) print('~ FPS : {:.2f}'.format(fps.fps()))
class PiVideoStream: def __init__(self, resolution=(400,200), framerate=60): #Start up camera self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.camera.rotation = -90 self.rawCap = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCap, format="bgr", use_video_port=True) self.frame = None self.stopped = False self.fps = FPS().start() def start(self): Thread(target=self.update, args=()).start() return self def update(self): # Continually grab frames from camera for f in self.stream: self.frame = f.array self.rawCap.truncate(0) self.fps.update() if self.stopped: self.stream.close() self.rawCap.close() self.camera.close() return def read(self): return self.frame def stop(self): self.fps.stop() self.stopped = True def getFPS(self): return self.fps.fps()
def __init__(self, exit_callback=None, is_show_video=True): self.exit_callback = exit_callback self.is_show_video = is_show_video print("[INFO] Recog Started | " "exit_callback: " + str(exit_callback) + " | Show Video: " + str(is_show_video)) print("[INFO] Loading encodings.pickle") self.data = pickle.loads( open(os.getcwd() + "/encodings.pickle", "rb").read()) print("[INFO] Loading Cascade Classifier") self.detector = cv2.CascadeClassifier( os.getcwd() + "/haarcascade_frontalface_default.xml") self.unlock = Unlock.Unlock() self.current_state = 0 self.needed_password = "" self.perfect_face_counter = 0 self.frame_label = "" self.led = led.Led() # initialize the video stream and allow the camera sensor to warm up print("[INFO] starting video stream...") vs = cv2.VideoCapture(0) self.vs = vs time.sleep(2.0) self.recognizeFaceInit() # start the FPS counter fps = FPS().start() # loop over frames from the video file stream while True: # grab the frame from the threaded video stream and resize it # to 500px (to speedup processing) ret, frame = vs.read() frame = imutils.resize(frame, width=500) self.frame = frame if self.current_state == self.STATE_RECOGNIZING_FACE: self.recognizeFaceUpdate() elif self.current_state == self.STATE_RECONGNIZING_QR: self.recognizeQRUpdate() elif self.current_state == self.STATE_UNLOCKING_DOOR: self.unlockDoorUpdate() if (self.is_show_video): cv2.putText(frame, self.frame_label, (12, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.6, (0, 255, 0), 1) # display the image to our screen cv2.imshow("Frame", self.frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): cv2.destroyAllWindows() vs.release() if self.exit_callback is not None: self.exit_callback() break elif key & 0xFF == 27: break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.release()
def recognize(inp_confidence, vid_file): # rpiName = socket.gethostname() # print(rpiName + "*************") global vs, outputFrame, lock, current, users # load our serialized face detector from disk print("[INFO] loading face detector...") protoPath = os.path.sep.join([FD_FOLDER, "deploy.prototxt"]) modelPath = os.path.sep.join( [FD_FOLDER, "res10_300x300_ssd_iter_140000.caffemodel"]) detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) # load our serialized face embedding model from disk print("[INFO] loading face recognizer...") embedder = cv2.dnn.readNetFromTorch(EMBEDDINGS_MODEL) # load the actual face recognition model along with the label encoder recognizer = pickle.loads(open(RECOGNIZER, "rb").read()) le = pickle.loads(open(LABEL_ENCODER, "rb").read()) # initialize the video stream, then allow the camera sensor to warm up print("[INFO] starting video stream...") # vs = VideoStream(src=0).start() vs = FileVideoStream(vid_file).start() time.sleep(2.0) # start the FPS throughput estimator fps = FPS().start() users = {} # loop over frames from the video file stream while True: # grab the frame from the threaded video stream frame = vs.read() frame = image_enhancer.image_enhance(frame) frame = image_enhancer.image_sharpen(frame) # sender.send_image(rpiName, cv2.resize(frame, (640,320))) # resize the frame to have a width of 600 pixels (while # maintaining the aspect ratio), and then grab the image # dimensions frame = imutils.resize(frame) (h, w) = frame.shape[:2] # construct a blob from the image imageBlob = cv2.dnn.blobFromImage(cv2.resize(frame, (720, 1280)), 1.0, (800, 800), (104.0, 177.0, 123.0), swapRB=False, crop=False) # apply OpenCV's deep learning-based face detector to localize # faces in the input image detector.setInput(imageBlob) detections = detector.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 if confidence > float(inp_confidence): # compute the (x, y)-coordinates of the bounding box for # the face box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # extract the face ROI face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] # ensure the face width and height are sufficiently large if fW < 20 or fH < 20: continue # construct a blob for the face ROI, then pass the blob # through our face embedding model to obtain the 128-d # quantification of the face faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) embedder.setInput(faceBlob) vec = embedder.forward() # face = cv2.resize(face, (160,160), interpolation = cv2.INTER_AREA) # sample = np.expand_dims(face, axis=0) # vec = model.predict(sample) # perform classification to recognize the face preds = recognizer.predict_proba(vec)[0] j = np.argmax(preds) proba = preds[j] name = le.classes_[j] current = name if not current in users: users[current] = 1 * proba else: users[current] = users[current] + 1 * proba # draw the bounding box of the face along with the # associated probability text = "{}: {:.2f}%".format(name, proba * 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) # update the FPS counter fps.update() with lock: outputFrame = frame.copy() # cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # 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 # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
max(0, d_ya), min(d_xb, W - 1), min(d_yb, H - 1) ] # Draw A Rectangle To Show The Detection cv2.rectangle(frame, (d_xa, d_ya), (d_xb, d_yb), (0, 255, 0), 2) # Update The FPS Counter fps.update() fps.stop() frame = cv2.resize(frame, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) # Display FPS On The Frame cv2.putText(frame, "FPS : " + "{:.2f}".format(fps.fps()), (10, 2 * H - 20), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 7) # Display The Frame cv2.imshow("Frame", frame) # Detect Keypress key = cv2.waitKey(1) & 0xFF # On Pressing 'q', Break From Infinite Loop if key == ord("q"): break # On Pressing 'c', Cancel The Current Tracking elif key == ord("c"): init = False
if key == 27: # exit break def get_model_instance_segmentation(num_classes): # load an instance segmentation model pre-trained pre-trained on COCO model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) # get number of input features for the classifier in_features = model.roi_heads.box_predictor.cls_score.in_features # replace the pre-trained head with a new one model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes) return model if __name__ == '__main__': model = get_model_instance_segmentation(4) model.load_state_dict(torch.load("../model/weights/model_with_no_mask.pth")) model.eval() model.to(device) torch.backends.cudnn.benchmark = True print("LOG : Load Weight Successfully") fps = FPS().start() startWebCam(model) # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. Prediction per second: {:.2f}".format(fps.fps())) # cleanup cv2.destroyAllWindows()
def gen(): from imutils.video import VideoStream from imutils.video import FPS import numpy as np import argparse import pickle import time import os import json import time from imutils import paths import numpy as np import argparse from sklearn.preprocessing import LabelEncoder from sklearn.svm import SVC ############################################################TAKING USER CREDENTIAL#################################################### #print("enter name") x = "Authorised" # Directory #directory = "{}".format(x) user_name = x count = 300 """ # Parent Directory path parent_dir = "dataset" path = os.path.join(parent_dir, directory) os.mkdir(path) print("Directory '% s' created" % directory) """ vs = VideoStream(src=0).start() fps = FPS().start() img_counter = 0 while True: frame = vs.read() img_name = "dataset/{}/0000{}.png".format(x, img_counter) cv2.imwrite(img_name, frame) #print("{} written!".format(img_name)) img_counter += 1 if img_counter == count: break frame = cv2.imencode('.jpg', frame)[1].tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # if the `q` key was pressed, break from the loop fps.stop() cv2.destroyAllWindows() vs.stop() print("Taken ----{}-------images".format(count)) ###################################################################################DETCTION MODEL####################################################### print("DETECTION MODEL START") # import the necessary packages ######################################extract_embeddings######################################################## # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) args["detector"] = "face_detection_model" args["dataset"] = "dataset" args["embedding_model"] = "openface_nn4.small2.v1.t7" args["embeddings"] = "output/embeddings.pickle" print( "--------------------------------extracting_embeddings-----------------------------------" ) # load our serialized face detector from disk print("[INFO] loading face detector...") protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"]) modelPath = os.path.sep.join( [args["detector"], "res10_300x300_ssd_iter_140000.caffemodel"]) detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) # load our serialized face embedding model from disk print("[INFO] loading face recognizer...") embedder = cv2.dnn.readNetFromTorch(args["embedding_model"]) # grab the paths to the input images in our dataset print("[INFO] quantifying faces...") imagePaths = list(paths.list_images(args["dataset"])) # initialize our lists of extracted facial embeddings and # corresponding people names knownEmbeddings = [] knownNames = [] # initialize the total number of faces processed total = 0 print("Extracting Features---------->>>") # loop over the image paths for (i, imagePath) in enumerate(imagePaths): # extract the person name from the image path name = imagePath.split(os.path.sep)[-2] # load the image, resize it to have a width of 600 pixels (while # maintaining the aspect ratio), and then grab the image # dimensions image = cv2.imread(imagePath) import imutils image = imutils.resize(image, width=600) (h, w) = image.shape[:2] # construct a blob from the image imageBlob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) # apply OpenCV's deep learning-based face detector to localize # faces in the input image detector.setInput(imageBlob) detections = detector.forward() # ensure at least one face was found if len(detections) > 0: # we're making the assumption that each image has only ONE # face, so find the bounding box with the largest probability i = np.argmax(detections[0, 0, :, 2]) confidence = detections[0, 0, i, 2] # ensure that the detection with the largest probability also # means our minimum probability test (thus helping filter out # weak detections) if confidence > args["confidence"]: # compute the (x, y)-coordinates of the bounding box for # the face box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # extract the face ROI and grab the ROI dimensions face = image[startY:endY, startX:endX] (fH, fW) = face.shape[:2] # ensure the face width and height are sufficiently large if fW < 20 or fH < 20: continue # construct a blob for the face ROI, then pass the blob # through our face embedding model to obtain the 128-d # quantification of the face faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) embedder.setInput(faceBlob) vec = embedder.forward() # add the name of the person + corresponding face # embedding to their respective lists knownNames.append(name) knownEmbeddings.append(vec.flatten()) total += 1 # dump the facial embeddings + names to disk print("[INFO] serializing {} encodings...".format(total)) data = {"embeddings": knownEmbeddings, "names": knownNames} f = open(args["embeddings"], "wb") f.write(pickle.dumps(data)) f.close() print("--------------------------TRAINING MODEL-----------------------") ####################################################################################################33 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-e", "--embeddings", required=False, help="path to serialized db of facial embeddings") ap.add_argument("-r", "--recognizer", required=False, help="path to output model trained to recognize faces") ap.add_argument("-l", "--le", required=False, help="path to output label encoder") args = vars(ap.parse_args()) args["embeddings"] = "output/embeddings.pickle" args["recognizer"] = "output/recognizer.pickle" args["le"] = "output/le.pickle" # load the face embeddings print("[INFO] loading face embeddings...") data = pickle.loads(open(args["embeddings"], "rb").read()) # encode the labels print("[INFO] encoding labels...") le = LabelEncoder() labels = le.fit_transform(data["names"]) # train the model used to accept the 128-d embeddings of the face and # then produce the actual face recognition print("[INFO] training model...") recognizer = SVC(C=1.0, kernel="linear", probability=True) recognizer.fit(data["embeddings"], labels) # write the actual face recognition model to disk f = open(args["recognizer"], "wb") f.write(pickle.dumps(recognizer)) f.close() # write the label encoder to disk f = open(args["le"], "wb") f.write(pickle.dumps(le)) f.close() ######################################################################################################### print( "--------------------------------RUNNING MAIN RECOGNITION FILE--------------------" ) #########################################yolo space################################################################ #################################################################################################### start_time = time.time() # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) args["detector"] = "face_detection_model" args["embedding_model"] = "openface_nn4.small2.v1.t7" args["recognizer"] = "output/recognizer.pickle" args["le"] = "output/le.pickle" # load our serialized face detector from disk print("[INFO] loading face detector...") protoPath = os.path.sep.join([args["detector"], "deploy.prototxt"]) modelPath = os.path.sep.join( [args["detector"], "res10_300x300_ssd_iter_140000.caffemodel"]) detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) # load our serialized face embedding model from disk print("[INFO] loading face recognizer...") embedder = cv2.dnn.readNetFromTorch(args["embedding_model"]) # load the actual face recognition model along with the label encoder recognizer = pickle.loads(open(args["recognizer"], "rb").read()) le = pickle.loads(open(args["le"], "rb").read()) ######################################################## ###################################################### # initialize the video stream, then allow the camera sensor to warm up print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) # start the FPS throughput estimator fps = FPS().start() flag = 0 flag_count = 1 # loop over frames from the video file stream while True: dictionary = { "Time_Stamp": 0, "Type": "Authorised Person", "Description": "Authorised Person is present" } f = 0 g = 0 # grab the frame from the threaded video stream frame = vs.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) end_time = time.time() dictionary["Time_Stamp"] = end_time - start_time #####################################phone with yolo ############################################################################# ######################################################################################################################################3333 # resize the frame to have a width of 600 pixels (while # maintaining the aspect ratio), and then grab the image # dimensions frame = imutils.resize(frame, width=600) (h, w) = frame.shape[:2] # construct a blob from the image imageBlob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) # apply OpenCV's deep learning-based face detector to localize # faces in the input image detector.setInput(imageBlob) detections = detector.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 if confidence > 0.7: # compute the (x, y)-coordinates of the bounding box for # the face box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # extract the face ROI face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] # ensure the face width and height are sufficiently large if fW < 20 or fH < 20: continue # construct a blob for the face ROI, then pass the blob # through our face embedding model to obtain the 128-d # quantification of the face faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) embedder.setInput(faceBlob) vec = embedder.forward() # perform classification to recognize the face preds = recognizer.predict_proba(vec)[0] j = np.argmax(preds) proba = preds[j] name = le.classes_[j] if name == user_name: name = "CORRECT USER" # draw the bounding box of the face along with the # associated probability text = name 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) if text == "unknown": cv2.putText(frame, "WARNING: UNAUTHORISED PERSON DETECTED", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) g = 1 flag = flag + 1 if flag > 100: img_name = "default/0000{}.png".format(flag_count) cv2.imwrite(img_name, frame) flag_count = flag_count + 1 time.sleep(0.5) flag = 0 # Serializing json if g == 1: dictionary["Type"] = "Unauthorised Person" dictionary["Description"] = "MULTIPLE PERSONS ARE DETECTED" json_object = json.dumps(dictionary, indent=4) # Writing to sample.json with open("sample.json", "a+") as outfile: outfile.write(json_object) # update the FPS counter fps.update() # show the output frame frame = cv2.imencode('.jpg', frame)[1].tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
break else: result_id = next_id results[result_id] = reid_result results_path[result_id] = [] next_id += 1 if debug: cv2.rectangle(debug_frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (10, 245, 10), 2) x = (bbox[0] + bbox[2]) // 2 y = (bbox[1] + bbox[3]) // 2 results_path[result_id].append([x, y]) cv2.putText(debug_frame, str(result_id), (x, y), cv2.FONT_HERSHEY_TRIPLEX, 1.0, (255, 255, 255)) if len(results_path[result_id]) > 1: cv2.polylines(debug_frame, [np.array(results_path[result_id], dtype=np.int32)], False, (255, 0, 0), 2) else: print(f"Saw id: {result_id}") if debug: aspect_ratio = frame.shape[1] / frame.shape[0] cv2.imshow("Camera_view", cv2.resize(debug_frame, (int(900), int(900 / aspect_ratio)))) if cv2.waitKey(1) == ord('q'): cv2.destroyAllWindows() break except KeyboardInterrupt: pass fps.stop() print("FPS: {:.2f}".format(fps.fps())) cap.release()
def ok(self): print("value is", self.e.get()) self.url = self.e.get() self.url = self.url + '/video' self.window3.destroy() window.destroy() CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # load our serialized model from disk print('[INFO] Loading Model...') net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print('[INFO] Starting Video Stream...') vs = VideoStream(self.url).start() time.sleep(2.0) fps = FPS().start() # loop over the frames from the video stream while True: frame = vs.read() frame = imutils.resize(frame, width=400) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) net.setInput(blob) detections = net.forward() for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > args["confidence"]: 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") label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100) if idx == 7: 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) cv2.imshow("Ip Camera Stream", frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break fps.update() fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vs.stop()
if writer is not None: # Checks if output should be written writer.write(frame) vis_util.save_processed('', frame) cv2.imshow("Frame", frame) # Window key = cv2.waitKey(1) & 0xFF if key == ord("q"): # Break from loop if 'q' is pressed break totalFrames += 1 # total frames fps.update() # FPS counter fps.stop() # Stops then displays Timer and and FPS print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) print("[INFO] detected persons: {}".format(detectCount)) print("[INFO] remaining battery: {}".format(bebop.sensors.battery)) if writer is not None: # release writer pointer if it exists writer.release() if not args.get("input", False): # If from drone, stop stream. stream.end_stream() print("[INFO]: STREAM ENDED") else: vs.release() # If not camera, stop reading video. cv2.destroyAllWindows() # Close windowss
class VideoCapture(QWidget): def __init__(self,parent): super(QWidget, self).__init__() self.cam = parent.cam self.fliphflags = parent.fliphlist self.flipvflags = parent.flipvlist self.video_frame = QLabel() self.FRAME_WIDTH = parent.CAM_WIDTH self.FRAME_HEIGHT = parent.CAM_HEIGHT if(parent.i>1): self.RECORD_WIDTH = self.FRAME_WIDTH*(parent.i-1) self.interface_cam_count = parent.i-1 else: self.RECORD_WIDTH = self.FRAME_WIDTH*3 self.interface_cam_count = parent.i self.view_1 = parent.view_1 self.view_2 = parent.view_2 self.view_3 = parent.view_3 self.RECORD_HEIGHT = self.FRAME_HEIGHT self.count=0 self.flag_record = 0 self.FPS = parent.FPS self.fourcc = cv2.VideoWriter_fourcc(*'DIVX') parent.layout.addWidget(self.video_frame) def nextFrameSlot(self): no_of_cam = [self.view_1,self.view_2,self.view_3] final_frame = 0 count=0 if(self.vs=="G"): for x in no_of_cam: #print("Self.View_"+str(count)+" = "+str(x)) if(count==0): ret, frame = self.cam[x].read() frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB) #============================================================================== # if(self.fliphflags[x]==1): # frame = cv2.flip(frame,1) # if(self.flipvflags[x]==1): # frame = cv2.flip(frame,0) #============================================================================== final_frame = frame else: ret, frame = self.cam[x].read() frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB) #frame = cv2.resize(frame,(self.FRAME_WIDTH, self.FRAME_HEIGHT),interpolation=cv2.INTER_CUBIC); #============================================================================== # if(self.fliphflags[x-1]==1): # frame = cv2.flip(frame,1) # if(self.flipvflags[x-1]==1): # frame = cv2.flip(frame,0) #============================================================================== final_frame = np.hstack((final_frame,frame)) count+=1 if(self.flag_record==1): self.out.write(final_frame) final_frame = cv2.resize(final_frame,(320*3, 240),interpolation=cv2.INTER_CUBIC); img = QImage(final_frame, final_frame.shape[1], final_frame.shape[0], QImage.Format_RGB888) pix = QPixmap.fromImage(img) self.video_frame.setPixmap(pix) self.count+=1 self.fps.update() #if(self.count%90==0): #print(self.count) def start(self,view_style,view_1,view_2,view_3): self.timer = QTimer() self.view_1 = view_1 self.view_2 = view_2 self.view_3 = view_3 self.fps = FPS().start() self.vs = view_style self.timer.timeout.connect(self.nextFrameSlot) print(self.FPS) self.timer.start(1000.0/self.FPS) def pause(self): print(self.FPS) self.timer.stop() self.fps.stop() print("[INFO] elasped time: {:.2f}".format(self.fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(self.fps.fps())) def start_record(self): print("Started recording") self.flag_record=1 timestamp = datetime.datetime.now().strftime('%Y_%m_%d(%H %M %S)') file_name = 'C:\\Users\\Sunny\\Desktop\\TRAINING_VIDEOS\\'+ timestamp +'.avi' print(file_name) self.out = cv2.VideoWriter(file_name,self.fourcc, 15, (self.RECORD_WIDTH,self.RECORD_HEIGHT)) def stop_record(self): print("Stopped recording") self.flag_record=0 self.out.release() def deleteLater(self): self.cap.release() super(QWidget, self).deleteLater() def wide_angle_stitching(self): print("WORK IN PROGRESS")
def gen(): #Loading Caffe Model print('[Status] Loading Model...') nn = cv2.dnn.readNetFromCaffe('SSD_MobileNet_prototxt.txt', 'SSD_MobileNet.caffemodel') #Initialize Video Stream (Use src = 0 for Webcam or src = 'path to video input') print('[Status] Starting Video Stream...') vs = VideoStream(src=0).start() #time.sleep(0.1) fps = FPS().start() #Loop Video Stream while True: #Resize Frame to 600 pixels frame = vs.read() frame = imutils.resize(frame, width=600) #Converting Frame to Blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) #Passing Blob through network to detect and predict nn.setInput(blob) detections = nn.forward() #Setting Focal Length F = 615 pos = {} coordinates = {} #Loop over the detections for i in np.arange(0, detections.shape[2]): #Extracting the confidence of predictions confidence = detections[0, 0, i, 2] #Filtering out weak predictions if confidence > 0.5: #Extracting the index of the labels from the detection object_id = int(detections[0, 0, i, 1]) #Identifying only Person as Detected Object if (object_id == 15): box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") #Draw the prediction on the frame label = "Person: {:.2f}%".format(confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), (10, 255, 0), 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_DUPLEX, 0.5, (20, 255, 0), 1) coordinates[i] = (startX, startY, endX, endY) #Mid point of bounding box midOfX = round((startX + endX) / 2, 4) midOfY = round((startY + endY) / 2, 4) ht = round(endY - startY, 4) #Distance from camera based on triangle similarity distance = (F * 165) / ht #Mid-point of bounding boxes (in cm) based on triangle similarity midOfX_cm = (midOfX * distance) / F midOfY_cm = (midOfY * distance) / F pos[i] = (midOfX_cm, midOfY_cm, distance) proximity = [] #Looping over positions of bounding boxes in frame for i in pos.keys(): for j in pos.keys(): if i < j: dist = sqrt( pow(pos[i][0] - pos[j][0], 2) + pow(pos[i][1] - pos[j][1], 2) + pow(pos[i][2] - pos[j][2], 2)) #Checking threshold distance - 175 cm if dist < 175: proximity.append(i) proximity.append(j) warning_label = "Maintain Safe Distance. Move away!" cv2.putText(frame, warning_label, (50, 50), cv2.FONT_HERSHEY_DUPLEX, 0.5, color, 1) for i in pos.keys(): if i in proximity: color = [0, 0, 255] else: color = [0, 255, 0] (x, y, w, h) = coordinates[i] cv2.rectangle(frame, (x, y), (w, h), color, 2) frame = cv2.imencode('.jpg', frame)[1].tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') key = cv2.waitKey(1) & 0xFF if key == ord("q"): break fps.update() fps.stop() print("[INFO]Elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO]Approx. FPS: {:.2f}".format(fps.fps()))
time.sleep(2.0) fps = FPS().start() # loop over some frames...this time using the threaded 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) # update the FPS counter fps.update() fps._end = datetime.datetime.now() # print(fps.fps()) cv2.putText(frame, str(fps.fps()), (50, 200), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 10) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break except KeyboardInterrupt: # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
def main(): # ---===--- Get the filename --- # '''filename = sg.popup_get_file('Filename to play') if filename: cap = cv2.VideoCapture(filename) #cap = WebcamVideoStream(src="http://192.168.0.101:8081").start() else: #vs = WebcamVideoStream(src=0).start() cap = cv2.VideoCapture(0)''' # ---===--- get stream --- # # cap = WebcamVideoStream(src=0).start() # cap = cv2.VideoCapture('actions1.mpg') # cap = WebcamVideoStream(src="http://192.168.0.100:8081").start() # ---===--- initialize --- # fps = FPS().start() count = 0 count_th = 20 rec = False sg.theme('Black') useVideo = False isFirst = True now = datetime.now() vodname = now.strftime("%m_%d_%Y,%H-%M-%S") cap = None # ---===--- define the window layout --- # tab1_layout = [ [ sg.T('Battery: '), sg.ProgressBar(1000, orientation='h', size=(10, 10), key='battbar'), sg.Text('Last detected: '), sg.Text(' ', key='-detected-', size=(20, 1)), sg.Button('Capture', key='-STOP-'), sg.Button('EXIT', key='-EXIT-') ], [sg.Image(filename='', key='-image-')], [ sg.Text('Height: ', size=(15, 1)), sg.Text('', size=(10, 1), justification='center', key='_HEIGHT_') ], [ sg.Text('Latitude: ', size=(15, 1)), sg.Text('', size=(10, 1), justification='center', key='_LATI_') ], [ sg.Text('Longitude: ', size=(15, 1)), sg.Text('', size=(10, 1), justification='center', key='_LONGTI_') ], [ sg.Text('FPS: '), sg.Text(size=(15, 1), key='-FPS-'), ], ] column1 = [ [ sg.Text('Detected history', background_color='#333333', justification='center', size=(20, 1)), ], #sg.Output(size=(40, 20))], [sg.Text('Gimbal command: '), sg.Text(size=(15, 1), key='-Gimbal-')] ] tab2_layout = [[sg.T('cropped/masked')], [ sg.Image(filename='', key='-cropped-'), sg.T(' ' * 30), sg.Image(filename='', key='-masked-'), sg.Column(column1, background_color='#333333') ], [ sg.Text('Size of the object: ', size=(15, 1)), sg.Text('', size=(10, 1), justification='center'), sg.Slider(range=(0, 500), default_value=15, size=(50, 10), orientation='h', key='-slider-') ]] tab3_layout = [[sg.Image(filename='', key='-histp-')], [sg.Text(' ', key='-history-', size=(20, 1))], [ sg.Button('Next'), sg.Button('Refresh'), sg.Button('Prev') ]] layout = [ [sg.Text('DemodetectionUI', size=(15, 1), font='Helvetica 20')], [ sg.TabGroup([[ sg.Tab('Cam view', tab1_layout), sg.Tab('Area view', tab2_layout), sg.Tab('Detected view', tab3_layout) ]], ) ], ] layoutWin2 = [ [sg.Text('Tracking and DetectionDemo', key='-STATUS-')], # note must create a layout from scratch every time. No reuse [ sg.Button('Start', key='-START-', disabled=True), sg.Button('Connect'), sg.Button('video'), sg.Checkbox('Record', key='-RECORD-', size=(12, 1), default=False) ], [sg.T(' ' * 12), sg.Button('Exit', )] ] # create the window and show it # main window window = sg.Window('DemoUI', layout, no_titlebar=False) # Open Connect Vods window window2 = sg.Window('DetectedHistory', layoutWin2, no_titlebar=False) # locate the elements we'll be updating. Does the search only 1 time image_elem = window['-image-'] cropped_elem = window['-cropped-'] masked_elem = window['-masked-'] # initializing stuffs mcd = MCDWrapper() # subtractor = cv2.createBackgroundSubtractorMOG2(history = 50,varThreshold=50,detectShadows=True) object_bounding_box = None cropped = None count, fcount, fmeter = 0, 0, 0 netip = '192.168.0.102' port = 25000 out = None s = None win2_active = True showhist = False frame = None event2, values2 = None, None capp = os.listdir("cap") ptr = 1 if capp: histpic = capp[len(capp) - ptr] while True: ev1, vals1 = window2.Read(timeout=100) if ev1 is None or ev1 == '-START-': window2.Close() win2_active = False if window2['-RECORD-'].Get(): fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('./recording/%s.avi' % (vodname), fourcc, 20.0, (1020, 720)) break if ev1 is None or ev1 == 'Exit': if out: out.release() break if ev1 is ev1 == 'video': filename = sg.popup_get_file('Filename to play') useVideo = True if filename: cap = cv2.VideoCapture(filename) else: cap = cv2.VideoCapture(0) if ev1 is ev1 == 'Connect': try: s = comm(netip, port) s = s.start() #cap = cv2.VideoCapture("http://192.168.0.102:8081") #streamer = ThreadedCamera("http://192.168.0.102:8081") cap = WebcamVideoStream( src="http://192.168.0.102:8081").start() time.sleep(1) except: sg.popup('Error CameraIP not found') if not cap: window2['-START-'].update(disabled=True) else: if useVideo: captype = 'Video' else: captype = 'Aerial camera' window2['-STATUS-'].update(captype) window2['-START-'].update(disabled=False) # ---===--- LOOP through video file by frame --- # while True and win2_active == False: event, values = window.read(timeout=20) if event in ('-EXIT-', None): if s: s.stop() if out: out.release() break if useVideo == True: if frame is not None: lastframe = frame.copy() try: ret, frame = cap.read() except: frame = lastframe else: #frame = streamer.grab_frame() if frame is not None: lastframe = frame.copy() try: frame = cap.read() except: frame = None while i <= 5 or frame: try: cap = WebcamVideoStream( src="http://192.168.0.102:8081").start() time.sleep(1) frame = cap.read() except: continue frame = lastframe i = 0 now = datetime.now() s1 = now.strftime("%m_%d_%Y,%H-%M-%S") objsize = int(values['-slider-']) logging.basicConfig(filename='Detected.log', format='%(asctime)s %(message)s', datefmt='%m_%d_%Y,%H-%M-%S') data = "X0_0_0_0" if s: s.getxyhf() data = s.getxyh() split = data.split('_') if len(split) > 3 and split[0][0] == 'X': window['_LATI_'].update(split[0][1:]) window['_LONGTI_'].update(split[1]) window['_HEIGHT_'].update(split[2]) window['battbar'].update_bar(int(float(split[3][0:2]))) if frame is None: # if out of data stop looping frame = lastframe if out: frameout = cv2.resize(frame, (1020, 720)) out.write(frameout) # resizing the big pic # frame = cv2.resize(frame,(1280,500)) # draw a bb around the obj # --------Event handler---------------- if event == '-STOP-': # stop video stop = window["-STOP-"] if frame.sum() > 0: object_bounding_box = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) object_tracker = cv2.TrackerMOSSE_create() object_tracker.init(frame, object_bounding_box) cv2.destroyAllWindows() if event == 'Refresh': capp = os.listdir("cap") ptr = 1 if capp: histpic = capp[len(capp) - ptr] histtemp = cv2.imread("./cap/%s" % (histpic)) histbytes = cv2.imencode('.png', histtemp)[1].tobytes() # ditto histpic = histpic.replace('-', ':') window['-histp-'].update(data=histbytes) window['-history-'].update(histpic.replace('.jpg', '')) if event == 'Prev' and capp: if ptr <= len(capp): ptr += 1 histpic = capp[len(capp) - ptr] histtemp = cv2.imread("./cap/%s" % (histpic)) histbytes = cv2.imencode('.png', histtemp)[1].tobytes() # ditto histpic = histpic.replace('-', ':') window['-histp-'].update(data=histbytes) window['-history-'].update(histpic.replace('.jpg', '')) if event == 'Next' and capp: if ptr > 1: ptr -= 1 histpic = capp[len(capp) - ptr] histtemp = cv2.imread("./cap/%s" % (histpic)) histbytes = cv2.imencode('.png', histtemp)[1].tobytes() # ditto histpic = histpic.replace('-', ':') window['-histp-'].update(data=histbytes) window['-history-'].update(histpic.replace('.jpg', '')) '''if event == '-detected-': # stop video capp = os.listdir("cap") tab3_layout = [[sg.T('Captured')], [sg.Listbox(values=capp,size=(20, 12), key='-LIST-')], [sg.Text(' ',key ='-File-'), sg.Button('Select',size=(15, 1))]] winchoose = sg.Window('Detected History', tab3_layout) event2, values2 = winchoose.Read() if event2 is not None and event2 == 'Select': filechoosen = True if values2['-LIST-'] and filechoosen == True: filechoosen = str(values2['-LIST-'][0]) winchoose.close() histtemp = cv2.imread("./cap/%s" % (filechoosen)) histbytes = cv2.imencode('.png', frame)[1].tobytes() # ditto winpic = sg.Window(filechoosen,[[sg.Image(data= histbytes, key='-histp-')],[sg.Button('Exit')]]) eventp, valuesp = winpic.read() winpic['-histp-'].update(data=histbytes) if eventp == 'Exit': filechoosen == False winpic.close()''' # --------bgsubtraction-----------# if object_bounding_box is not None: success, object_bounding_box = object_tracker.update(frame) if success: (x, y, w, h) = [int(v) for v in object_bounding_box] cropped = frame[y:y + h, x:x + w].copy() cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) #detectsize = objsize/2 cv2.rectangle(frame, (x + objsize, y + objsize), (x + w - objsize, y + h - objsize), (0, 0, 255), 2) if (y + (h / 2) - 50) > frame.shape[0] / 2: # order gimbal if count >= count_th: inp = "u" if s: s.setinp(inp) s.sendcommand() window['-Gimbal-'].update(inp) print((y + h) / 2, frame.shape[0]) count = 0 elif (y + (h / 2)) + 50 < frame.shape[0] / 2: if count >= count_th: inp = "d" if s: s.setinp(inp) s.sendcommand() print((y + h) / 2, frame.shape[0]) window['-Gimbal-'].update(inp) count = 0 else: if count >= count_th: inp = "s" if s: s.setinp(inp) s.sendcommand() print((y + h) / 2, frame.shape[0]) window['-Gimbal-'].update(inp) count = 0 count += 1 if cropped is not None and cropped.sum() > 0: # resizing the small pic # cropped = cv2.resize(cropped,(600,400)) # cv2.imwrite("./all/frame%d.jpg" % count, cropped) count += 1 # mask = subtractor.apply(cropped) # for some reason the fast MCD accepts only the %4 size if cropped.shape[0] % 4 != 0 or cropped.shape[1] % 4 != 0: cropped = cv2.resize(cropped, ((cropped.shape[1] // 4) * 4, (cropped.shape[0] // 4) * 4)) gray = cv2.cvtColor(cropped, cv2.COLOR_RGB2GRAY) mask = np.zeros(gray.shape, np.uint8) if (isFirst): mcd.init(gray) isFirst = False else: try: mask = mcd.run(gray) except: mcd.init(gray) # draw contours (cnts, _) = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for contour in cnts: if (cv2.contourArea(contour) > objsize): cv2.drawContours(cropped, contour, -1, (0, 255, 0), 2) (x, y, w, h) = cv2.boundingRect(contour) cv2.rectangle(cropped, (x, y), (x + w, y + h), (0, 255, 0), 3) ##if cv2.contourArea(contour)>50: # print(contour) # cv2.drawContours(cropped,contour,-1,(0,255,0),2) if x <= objsize / 2 or x >= cropped.shape[ 1] - objsize / 2 or x + w <= objsize / 2 or x + w >= cropped.shape[ 1] - objsize / 2: if fmeter >= 3: cv2.imwrite("./capc/%s.jpg" % (s1), cropped) cv2.imwrite("./cap/%s.jpg" % (s1), frame) logging.warning(" ") fcount += 1 window['-detected-'].update(s1) count += 1 fmeter = 0 else: fmeter += 1 elif y <= objsize / 2 or y >= cropped.shape[ 0] - objsize / 2 or y + h <= objsize / 2 or y + h >= cropped.shape[ 0] - objsize / 2: if fmeter >= 3: cv2.imwrite("./capc/%s.jpg" % (s1), cropped) cv2.imwrite("./cap/%s.jpg" % (s1), frame) logging.warning(" ") fcount += 1 window['-detected-'].update(s1) count += 1 fmeter = 0 else: fmeter += 1 # showing imgbytes = cv2.imencode('.png', frame)[1].tobytes() # ditto image_elem.update(data=imgbytes) fps.update() fps.stop() window['-FPS-'].update("{:.2f}".format(fps.fps())) if cropped is not None: try: # print(cropped.shape) imgcropped = cv2.imencode('.png', cropped)[1].tobytes() imgmarked = cv2.imencode('.png', mask)[1].tobytes() cropped_elem.update(data=imgcropped) masked_elem.update(data=imgmarked) except: continue
avg = avgframe(lis) stack.append(work(avg)) cam.get_image(img) frame = img.get_image_data_numpy() frame = cv2.flip(frame, 0) # flip the frame vertically frame = cv2.flip(frame, 1) stackref.append(work(frame[int(roiref[1]):int(roiref[1]+roiref[3]), int(roiref[0]):int(roiref[0]+roiref[2])])) print(len(stack),len(stackref)) with open('stack.pkl', 'wb') as f: load=[stack,stackref,roimain,roiref] pickle.dump(load, f) f.close() tmc_dac.write("INST:NSEL 1") tmc_dac.write("VOLT %.3f"%((KEITHLEY1_VALUE - KEITHLEY1_VALUE_STEPSIZE*stacksize/2))) fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) cam.stop_acquisition() cam.close_device() os._exit(1) # sys.exit()
window_name = 'preview' # Creation du thread de lecture + setup vs=PiVideoStream() vs.camera.video_stabilization = True # Demarrage du flux video + warmup de la camera vs.start() time.sleep(2.0) # Creation de la fenetre d'affichage cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE) fps = FPS().start() while True : frame = vs.read() fps.update() cv2.imshow(window_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord("q") : break fps.stop() print("Temps passé : {:.2f}".format(fps.elapsed())) print("Approx. FPS : {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vs.stop()
writer.write(frame) # show the output frame cv2.imshow("Traffic Counter System", frame) key = cv2.waitKey(1) & 0xFF frame_counter += 1 # if the `q` key was pressed, break from the loop if key == ord("q"): break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] rata-rata total FPS: {:.2f}".format(fps.fps())) # check to see if we need to release the video writer pointer if writer is not None: writer.release() # if we are not using a video file, stop the camera video stream if not args.get("input", False): vs.stop() # otherwise, release the video file pointer else: vs.release() #close all the frames cv2.destroyAllWindows()
def start_processing(self): if self.input_source is not None: file_stream = FileVideoStream( self.input_source, queue_size=self.app_imutils_queue_size).start() time.sleep(0.001) detector = self.initializeDetector() self.tracker = self.initializeTracker() self.setDataset() fps = FPS().start() frame_id = 0 all_boxes = {} tracking_boxes = [] while (not self.source_changed) and file_stream.running(): time.sleep(0.001) try: self.image = file_stream.read() if frame_id % self.app_process_every_nth_frame == 0: if (self.image is not None): vis = self.image.copy() cls_boxes = None cls_segms = None cls_keyps = None timers = defaultdict(Timer) t = time.time() fps.update() fps.stop() self.logger.info('Processing file {}'.format( self.input_source)) self.logger.info( 'Processing frame {}'.format(frame_id)) fps_text = "FPS " + "{:.2f}".format(fps.fps()) self.logger.info('FPS: ' + fps_text) if self.app_do_detection and not self.source_changed: cls_boxes, cls_segms, cls_keyps = self.infer( vis, timers, detector) all_boxes[frame_id] = cls_boxes self.logger.info( 'Inference time: {:.3f}s'.format( time.time() - t)) for k, v in timers.items(): self.logger.info(' | {}: {:.3f}s'.format( k, v.average_time)) if frame_id == 0: self.logger.info( ' \ Note: inference on the first image will be slower than the ' 'rest (caches and auto-tuning need to warm up)' ) fps_text = "FPS " + "{:.2f}".format(fps.fps()) self.logger.info('FPS: ' + fps_text) if self.app_display_det_result_img: if frame_id % self.app_display_det_every_nth_frame == 0: vis = self.visualize_det( vis, cls_boxes, fps_text, segms=cls_segms, keypoints=cls_keyps) if self.app_save_det_result_img: if not self.app_display_det_result_img: ret = self.visualize_det( vis, cls_boxes, fps_text, segms=cls_segms, keypoints=cls_keyps) self.save_det_result_img(ret, frame_id) else: self.save_det_result_img(vis, frame_id) if self.app_do_tracking and not App.is_list_empty( cls_boxes) and not self.source_changed: t = time.time() tmp_tracking_boxes = self.track( self.image.copy(), cls_boxes, frame_id, timers) self.logger.info( 'Tracking time (incl. feature generation): {:.3f}s' .format(time.time() - t)) if self.app_display_tracking_result_img: if frame_id % self.app_display_tracking_every_nth_frame == 0: vis = self.visualize_tracking( vis, tmp_tracking_boxes, fps_text) if self.app_save_tracking_result_img: if not self.app_display_tracking_result_img: ret = self.visualize_tracking( vis, tmp_tracking_boxes, fps_text) self.save_tracking_result_img( ret, frame_id) else: self.save_tracking_result_img( vis, frame_id) tracking_boxes = self.extend_result_boxes( frame_id, tracking_boxes, tmp_tracking_boxes) if self.app_display: cv2.imshow('source', vis) ch = 0xFF & cv2.waitKey(1) if ch == 27: break self.logger.info( 'Total time frame {}: {:.3f}s'.format( frame_id, time.time() - t)) frame_id += 1 except Exception: print(sys.exc_info()[0] + sys.exc_info()[1]) #continue if self.app_save_det_result_boxes: self.save_det_result_boxes(all_boxes) self.logger.info('Wrote detections to: {}'.format( os.path.abspath(self.app_save_det_result_path))) if self.app_save_tracking_result_boxes: self.save_tracking_result_boxes(list(tracking_boxes)) self.logger.info('Wrote tracks to: {}'.format( os.path.abspath(self.app_save_tracking_result_path))) file_stream.stop() self.source_changed = False if not self.bulk_processing: self.start_processing() else: self.root.quit()
elif state == "missing" and not shape_found: if float((cv2.getTickCount() - start_time_missing)) / cv2.getTickFrequency() > maximum_missing_time: state = "stopped" if ser is not None: ser.write("0:0" + "\n") elif state == "stopped" and not shape_found: pass if ser is not None: status2 += " " + ser.readline() + " " + state fps.update() fps.stop() cv2.putText(gray, ip + "fps: " + str(int(fps.fps())), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) cv2.putText(gray, status, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) cv2.putText(gray, status2, (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) cv2.imshow("Frame", np.hstack([gray, edged])) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break # cleanup the camera and close any open windows if ser is not None: ser.close() video.stop() cv2.destroyAllWindows()
def liveStream(): CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") vs = VideoStream().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=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)), 0.007843, (300, 300), 127.5) # 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 np.arange(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"]: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object 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") # draw the prediction on the frame label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100) if idx == 7: 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) # show the output frame cv2.imshow("Live Stream", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
(255, 255, 255), -1) blurred = cv2.GaussianBlur(frame, (51, 51), 11) frame = np.where(mask == np.array([255, 255, 255]), blurred, frame) elif success: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # update the FPS counter fps.update() fps.stop() # initialize the set of information we'll be displaying on # the frame info = [ ("Tracker", args["tracker"]), ("Success", "Yes" if success else "No"), ("FPS", "{:.2f}".format(fps.fps())), ] if args.get("stat", False): # 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) cv2.imshow("Face Recognizer", frame) # Press ESC or 'q' to quit the program key = cv2.waitKey(1) & 0xff if key == 27 or key == ord('q'): break
def main(): fps = FPS().start() cap = cv2.VideoCapture(Input_Video) YOLOINIT() ##========================================================= ##View 1 f_num = 0 Detecting_cnt_1 = 0 RED_cnt_1 = 0 BLUE_cnt_1 = 0 initBB_1 = None tracker_1 = None while (cap.isOpened()): f_num = f_num + 1 print("F : ", f_num) (grabbed, frame) = cap.read() if f_num % 2 == 0: #====================================================== #Background Substraction #tracker 에 대해서 frame 원본이미지가 아니라, Background Substracted 된 영상에 트래커를 부착하여, 배경에 트래커가 남지 않도록 구현 gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0) difference = cv2.absdiff(first_gray, gray_frame) _, difference = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY) mask3 = cv2.cvtColor(difference, cv2.COLOR_GRAY2BGR) # 3 channel mask Substracted = cv2.bitwise_and(frame, mask3) #====================================================== layerOutputs, start, end = YOLO_Detect(frame) # 3.YOLO_BOX_INFO(layerOutputs,BaseConfidence,Base_threshold)) idxs, boxes, classIDs, confidences = YOLO_BOX_INFO( frame, layerOutputs, BaseConfidence, Base_threshold) # 4.검출된 화면의 X,Y 좌표 가져온다. # 검출됨 차량 수 만큼 좌표 가져옴 Vehicle_x = [] Vehicle_y = [] Vehicle_w = [] Vehicle_h = [] #차량 포인트 가져옴 Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h = Position( idxs, classIDs, boxes, Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h) #차량 포인트 그리기 -> yolo detection bounding box Draw_Points(frame, Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h) #4개의 포인트 vertices = [[[450, 700], [460, 900], [1500, 900], [950, 700]]] tracker_1, initBB_1, RED_cnt_1, BLUE_cnt_1 = Passing_Counter_Zone(Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h, initBB_1, frame, tracker_1, Substracted,\ RED_cnt_1, BLUE_cnt_1, vertices) # Red_Line #cv2.line(frame, (vertices[0][0][0], vertices[0][0][1]), (vertices[0][3][0], vertices[0][3][1]), (0, 0, 255), 2) #cv2.putText(frame, "IN Cnt : " + str(RED_cnt_1), (vertices[0][0][0], vertices[0][0][1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) # Blue_Line cv2.line(frame, (vertices[0][1][0], vertices[0][1][1]), (vertices[0][2][0], vertices[0][2][1]), (255, 0, 0), 2) cv2.putText(frame, "IN Cnt : " + str(BLUE_cnt_1), (vertices[0][1][0], vertices[0][1][1] + 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3) # Detecting Zone pts_1 = detecting_zone(vertices) cv2.polylines(frame, [pts_1], True, (0, 255, 0), 2) #프레임 레터박스 blank_image = np.zeros((64, 1920, 3), np.uint8) frame[0:64, 0:1920] = blank_image frame = cv2.resize( frame, (1280, 720), interpolation=cv2.INTER_CUBIC) #1920, 1080 -> 1280,720 Substracted = cv2.resize(Substracted, (1280, 720), interpolation=cv2.INTER_CUBIC) fps.update() fps.stop() cv2.putText(frame, "FPS : " + "{:.2f}".format(fps.fps()), (25, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.putText(frame, "Car count : " + "{}".format(BLUE_cnt_1), (500, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.imshow("frame", frame) cv2.imshow("sub", Substracted) if cv2.waitKey(1) & 0xFF == ord('q'): break return
def run(): # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--prototxt", required=False, help="path to Caffe 'deploy' prototxt file") ap.add_argument("-m", "--model", required=True, help="path to Caffe pre-trained model") ap.add_argument("-i", "--input", type=str, help="path to optional input video file") ap.add_argument("-o", "--output", type=str, help="path to optional output video file") # confidence default 0.4 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 MobileNet SSD was trained to # detect 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 disk net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # if a video path was not supplied, grab a reference to the ip camera if not args.get("input", False): print("[INFO] Starting the live stream..") vs = VideoStream(config.url).start() time.sleep(2.0) # otherwise, grab a reference to the video file else: print("[INFO] Starting the video..") vs = cv2.VideoCapture(args["input"]) # initialize the video writer (we'll instantiate later if need be) writer = None # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) W = None H = None # instantiate our centroid tracker, then initialize a list to store # each of our dlib correlation trackers, followed by a dictionary to # map each unique object ID to a TrackableObject ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} # initialize the total number of frames processed thus far, along # with the total number of objects that have moved either up or down totalFrames = 0 totalDown = 0 totalUp = 0 x = [] empty = [] empty1 = [] # start the frames per second throughput estimator fps = FPS().start() if config.Thread: vs = thread.ThreadingClass(config.url) # loop over frames from the video stream while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream frame = vs.read() frame = frame[1] if args.get("input", 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 args["input"] is not None and frame is None: break # resize the frame to have a maximum width of 500 pixels (the # less data we have, the faster we can process it), then convert # the frame from BGR to RGB for dlib frame = imutils.resize(frame, width=500) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] # if we are supposed to be writing a video to disk, initialize # the writer if args["output"] is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(args["output"], fourcc, 30, (W, H), True) # initialize the current status along with our list of bounding # box rectangles returned by either (1) our object detector or # (2) the correlation trackers status = "Waiting" rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % args["skip_frames"] == 0: # set the status and initialize our new set of object trackers status = "Detecting" trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5) net.setInput(blob) detections = net.forward() # loop over the detections for i in np.arange(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 requiring a minimum # confidence if confidence > args["confidence"]: # extract the index of the class label from the # detections list idx = int(detections[0, 0, i, 1]) # if the class label is not a person, ignore it if CLASSES[idx] != "person": 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") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker tracker = dlib.correlation_tracker() rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(rgb, rect) # add the tracker to our list of trackers so we can # utilize it during skip frames trackers.append(tracker) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput else: # loop over the trackers for tracker in trackers: # set the status of our system to be 'tracking' rather # than 'waiting' or 'detecting' status = "Tracking" # update the tracker and grab the updated position tracker.update(rgb) pos = tracker.get_position() # unpack the position object startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) # add the bounding box coordinates to the rectangles list rects.append((startX, startY, endX, endY)) # draw a horizontal line in the center of the frame -- once an # object crosses this line we will determine whether they were # moving 'up' or 'down' cv2.line(frame, (0, H // 2), (W, H // 2), (0, 0, 0), 3) cv2.putText(frame, "-Prediction border - Entrance-", (10, H - ((i * 20) + 200)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) # use the centroid tracker to associate the (1) old object # centroids with (2) the newly computed object centroids objects = ct.update(rects) # loop over the tracked objects for (objectID, centroid) in objects.items(): # check to see if a trackable object exists for the current # object ID to = trackableObjects.get(objectID, None) # if there is no existing trackable object, create one if to is None: to = TrackableObject(objectID, centroid) # otherwise, there is a trackable object so we can utilize it # to determine direction else: # the difference between the y-coordinate of the *current* # centroid and the mean of *previous* centroids will tell # us in which direction the object is moving (negative for # 'up' and positive for 'down') y = [c[1] for c in to.centroids] direction = centroid[1] - np.mean(y) to.centroids.append(centroid) # check to see if the object has been counted or not if not to.counted: # if the direction is negative (indicating the object # is moving up) AND the centroid is above the center # line, count the object if direction < 0 and centroid[1] < H // 2: totalUp += 1 empty.append(totalUp) to.counted = True # if the direction is positive (indicating the object # is moving down) AND the centroid is below the # center line, count the object elif direction > 0 and centroid[1] > H // 2: totalDown += 1 empty1.append(totalDown) #print(empty1[-1]) x = [] # compute the sum of total people inside x.append(len(empty1) - len(empty)) #print("Total people inside:", x) # if the people limit exceeds over threshold, send an email alert if sum(x) >= config.Threshold: cv2.putText(frame, "-ALERT: People limit exceeded-", (10, frame.shape[0] - 80), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 2) if config.ALERT: print("[INFO] Sending email alert..") Mailer().send(config.MAIL) print("[INFO] Alert sent") to.counted = True # store the trackable object in our dictionary trackableObjects[objectID] = to # draw both the ID of the object and the centroid of the # object on the output frame text = "ID {}".format(objectID) cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.circle(frame, (centroid[0], centroid[1]), 4, (255, 255, 255), -1) # construct a tuple of information we will be displaying on the info = [ ("Exit", totalUp), ("Enter", totalDown), ("Status", status), ] info2 = [ ("Total people inside", x), ] # Display the output 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, 0), 2) for (i, (k, v)) in enumerate(info2): text = "{}: {}".format(k, v) cv2.putText(frame, text, (265, H - ((i * 20) + 60)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2) # Initiate a simple log to save data at end of the day if config.Log: datetimee = [datetime.datetime.now()] d = [datetimee, empty1, empty, x] export_data = zip_longest(*d, fillvalue='') with open('Log.csv', 'w', newline='') as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(("End Time", "In", "Out", "Total Inside")) wr.writerows(export_data) # show the output frame cv2.imshow("Real-Time Monitoring/Analysis Window", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # increment the total number of frames processed thus far and # then update the FPS counter totalFrames += 1 fps.update() if config.Timer: # Automatic timer to stop the live stream. Set to 8 hours (28800s). t1 = time.time() num_seconds = (t1 - t0) if num_seconds > 28800: break # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # # if we are not using a video file, stop the camera video stream # if not args.get("input", False): # vs.stop() # # # otherwise, release the video file pointer # else: # vs.release() # close any open windows cv2.destroyAllWindows()
def multiple_detection(vs, num): #vs= VideoStream(src=0).start() time.sleep(2.0) # otherwise, grab a reference to the video file # initialize the video writer (we'll instantiate later if need be) writer = None # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) W = None H = None # instantiate our centroid tracker, then initialize a list to store # each of our dlib correlation trackers, followed by a dictionary to # map each unique object ID to a TrackableObject ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} # initialize the total number of frames processed thus far, along # with the total number of objects that have moved either up or down totalFrames = 0 totalDown = 0 totalUp = 0 # start the frames per second throughput estimator fps = FPS().start() # loop over frames from the video stream while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream frame = vs.read() frame = frame[1] if args.get("input", 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 args["input"] is not None and frame is None: break # resize the frame to have a maximum width of 500 pixels (the # less data we have, the faster we can process it), then convert # the frame from BGR to RGB for dlib frame = imutils.resize(frame, width=500) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] # if we are supposed to be writing a video to disk, initialize # the writer # if args["output"] is not None and writer is None: # fourcc = cv2.VideoWriter_fourcc(*"MJPG") # writer = cv2.VideoWriter(args["output"], fourcc, 30, # (W, H), True) # initialize the current status along with our list of bounding # box rectangles returned by either (1) our object detector or # (2) the correlation trackers status = "Waiting" rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % args["skip_frames"] == 0: # set the status and initialize our new set of object trackers status = "Detecting" cam_list[num] = 0 trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5) net.setInput(blob) detections = net.forward() # loop over the detections for i in np.arange(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 requiring a minimum # confidence if confidence > args["confidence"]: # extract the index of the class label from the # detections list idx = int(detections[0, 0, i, 1]) # if the class label is not a person, ignore it if CLASSES[idx] != "person": 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") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker tracker = dlib.correlation_tracker() rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(rgb, rect) # add the tracker to our list of trackers so we can # utilize it during skip frames trackers.append(tracker) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput else: # loop over the trackers for tracker in trackers: # set the status of our system to be 'tracking' rather # than 'waiting' or 'detecting' status = "Detected" cam_list[num] = 1 # update the tracker and grab the updated position tracker.update(rgb) pos = tracker.get_position() # unpack the position object startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) # add the bounding box coordinates to the rectangles list rects.append((startX, startY, endX, endY)) # draw a horizontal line in the center of the frame -- once an # object crosses this line we will determine whether they were # moving 'up' or 'down' cv2.line(frame, (0, H), (W, H), (0, 255, 255), 2) # use the centroid tracker to associate the (1) old object # centroids with (2) the newly computed object centroids objects = ct.update(rects) # loop over the tracked objects for (objectID, centroid) in objects.items(): # check to see if a trackable object exists for the current # object ID to = trackableObjects.get(objectID, None) # if there is no existing trackable object, create one if to is None: to = TrackableObject(objectID, centroid) # otherwise, there is a trackable object so we can utilize it # to determine direction # else: # # the difference between the y-coordinate of the *current* # # centroid and the mean of *previous* centroids will tell # # us in which direction the object is moving (negative for # # 'up' and positive for 'down') # y = [c[1] for c in to.centroids] # direction = centroid[1] - np.mean(y) # to.centroids.append(centroid) # # check to see if the object has been counted or not # if not to.counted: # # if the direction is negative (indicating the object # # is moving up) AND the centroid is above the center # # line, count the object # #if direction < 0 and centroid[1] < H // 2: # totalUp += 1 # to.counted = True # # if the direction is positive (indicating the object # # is moving down) AND the centroid is below the # # center line, count the object # # elif direction > 0 and centroid[1] > H // 2: # # totalDown += 1 # # to.counted = True # # store the trackable object in our dictionary # trackableObjects[objectID] = to # # draw both the ID of the object and the centroid of the # # object on the output frame # text = "ID {}".format(objectID) # cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 255, 0), -1) # construct a tuple of information we will be displaying on the # frame # info = [ # ("Countrer", totalUp), # ("Status", status), # ] # loop over the info tuples and draw them on our frame # for (i, k) 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) print("Status:", status) # print("counter",i) # check to see if we should write the frame to disk if writer is not None: writer.write(frame) # 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 # increment the total number of frames processed thus far and # then update the FPS counter totalFrames += 1 fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # check to see if we need to release the video writer pointer if writer is not None: writer.release() # if we are not using a video file, stop the camera video stream if not args.get("input", False): vs.stop() # otherwise, release the video file pointer else: vs.release() # close any open windows cv2.destroyAllWindows()
if servo1 < 150: servo1 = servo1 + 2 os.system("echo 6=%s > /dev/servoblaster" % servo1) time.sleep(0.005) elif key == 84: if servo2 < 150: servo2 = servo2 + 2 os.system("echo 5=%s > /dev/servoblaster" % servo2) time.sleep(0.005) # Update the FPS counter fps.update() # Stop the timer and display FPS information fps.stop() print("Elasped time: {:.2f}".format(fps.elapsed())) print("Approx. FPS: {:.2f}".format(fps.fps())) #Update Attendance print(u_names) if (len(u_names) != 0): if (len(u_names) == 1 and 'Unknown' in u_names): update(None, "No recognizable students present!") else: update(u_names, "Attendance Records Updated!") else: update(None, "No students present!") # Do cleanup cv2.destroyAllWindows() vs.stop()
def start_detection(self, camid=0): # load classifier pickle_in = open(self.base_dir+"knn_clf.pickle","rb") classifier = pickle.load(pickle_in) pickle_in.close() # setup camera to capture video video_capture = cv2.VideoCapture(camid) # start frames capturing timer fps = FPS().start() # start indefinite loop for video capturing while True: # fetch camera frame ret, frame = video_capture.read() '''ret=True url='http://192.168.43.79:8086/shot.jpg' imgResp=urllib.request.urlopen(url) imgNp=np.array(bytearray(imgResp.read()),dtype=np.uint8) frame=cv2.imdecode(imgNp,-1) ''' # validate if image is captured, else stop video capturing if ret!=True: print("\nCamera not detected") video_capture.release() cv2.destroyAllWindows() return ################################## # apply image processing ####################################### #frame = imutils.resize(frame, height=200) # load model parameters blob = cv2.dnn.blobFromImage(frame, 1 / 255, (416, 416), [0, 0, 0], 1, crop=False) self.net.setInput(blob) # fetch model predictions layers_names = self.net.getLayerNames() outs = self.net.forward([layers_names[i[0] - 1] for i in self.net.getUnconnectedOutLayers()]) # fetch captured image dimensions (frame_height, frame_width) = frame.shape[:2] # declare confidences, bounding boxes and face location bounding boxes list confidences = [] boxes = [] face_locations = [] # looping through grid cells for out in outs: # looping through detectors for detection in out: # fetch classes probability scores = detection[5:] # fetch class with maximum probability class_id = np.argmax(scores) # fetch maximum probability confidence = scores[class_id] # filter prediction based on threshold value if confidence > self.yolo_conf_threshold: # fetch validated bounding boxes center_x = int(detection[0] * frame_width) center_y = int(detection[1] * frame_height) width = int(detection[2] * frame_width) height = int(detection[3] * frame_height) left = int(center_x - width / 2) top = int(center_y - height / 2) # add confidences and bounding boxes in list confidences.append(float(confidence)) boxes.append([left, top, width, height]) # perform non maximum suppression to remove overlapping images based on nms_threshold value indices = cv2.dnn.NMSBoxes(boxes, confidences, self.yolo_conf_threshold, self.nms_threshold) # fetch legitimate face bounding boxes for i in indices: i = i[0] box = boxes[i] left = box[0] top = box[1] width = box[2] height = box[3] face_locations.append(np.array([top, left+width, top+height, left ])) gamma=2 invGamma = 1.0 / gamma table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8") frame1 = cv2.LUT(frame, table) img_to_yuv = cv2.cvtColor(frame1, cv2.COLOR_BGR2YUV) img_to_yuv[:,:,0] = cv2.equalizeHist(img_to_yuv[:,:,0]) frame1 = cv2.cvtColor(img_to_yuv, cv2.COLOR_YUV2BGR) # encode faces to be fed to classifier for prediction face_encodings = face_recognition.face_encodings(frame, face_locations, num_jitters=1) count=0 # loop through face encodings and face boundary boxes for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): face_encoding = [face_encoding] #probabilities = classifier.predict_proba(face_encoding) predictions = classifier.kneighbors(face_encoding, n_neighbors=100)#classifier.n_neighbors) dist_name = defaultdict(list) [dist_name[self.known_face_names[key]].append(value) for value, key in zip(predictions[0][0], predictions[1][0])] # sort dictionary based on number of values dist_name = sorted(dist_name.items(), key=lambda item: len(item[1]), reverse=True) # fetch average distance of top class from given image avg_distance = round(sum(dist_name[0][1])/len(dist_name[0][1]),2) confidence = (len(dist_name[0][1])/100)#classifier.n_neighbors) name = "Unknown" if (avg_distance <= self.knn_distance_threshold and confidence>=self.classifier_threshold) or (avg_distance <= self.knn_distance_threshold+0.08 and confidence>=0.8): name = dist_name[0][0] # fetch maximum probability value #confidence = max(probabilities[0]) #print(classifier.classes_[np.argmax(probabilities)], confidence, max(confidences)) # set name as unknown if confidence is lower than threshold value #name = "Unknown" #if confidence>self.classifier_threshold: # # fetch class with maximum probability # name = classifier.classes_[np.argmax(probabilities)] # print("\n[INFO] Probabilities:", probabilities[0], probability*100, avg_distance, "\nName:", name, "Name1:", dist_name[0][0]) print("[INFO] Average Distance:", avg_distance) print("[INFO] Probability:", confidence) print("[INFO] Class:", dist_name[0][0], "\n") # Draw a box around the face cv2.rectangle(frame, (left, top), (right, bottom), (255, 0, 0), 2) # Draw a label with a name below the face cv2.rectangle(frame, (left, bottom), (right, bottom+20), (255, 0, 0), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name+','+dist_name[0][0], (left + 6, bottom+15), font, 0.5, (255, 255, 255), 1) if name!='Unknown': # put label for confidence cv2.rectangle(frame, (left, top-20), (right, top), (255, 0, 0), cv2.FILLED) cv2.putText(frame, str(round(confidence*100,2))+'%', (left + 6, top-3), font, 0.5, (255, 255, 255), 1) count+=1 # Display the resulting image cv2.imshow('Face Recognition', frame) # Hit 'q' on the keyboard to quit! if cv2.waitKey(1) & 0xFF == ord('q'): break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # Release handle to the webcam cv2.destroyAllWindows() video_capture.release()
speed = int(pidout) + 1000 if enable: #if framecounter % 2 == 0: if s.out_waiting > 10: reset_output_buffer() else: #Send Update if PID has changed by threshold value if (speed + sendthresh) > sendold or (speed - sendthresh) < sendold: sendold = speed s.write((str(speed) + "\n").encode()) f.write( str(angle) + "," + str(pidout) + "," + str(speed) + "," + str(fps.fps()) + "," + str(fps.elapsed()) + "\n") else: if framecounter % 20 == 0: s.write("0\n".encode()) s.flush() s.flushOutput() #PID_Parameters[0] = cv2.getTrackbarPos('kP', 'CV PID Settings') #PID_Parameters[1]= cv2.getTrackbarPos('ki', 'CV PID Settings') #PID_Parameters[2] = cv2.getTrackbarPos('kd', 'CV PID Settings') else: #Not Found angle = 0 pidout = 0 speed = 0 dir = 0
def video_stream(face_detector, arcface_classifier, unknown_folder, logs_folder): cap = cv2.VideoCapture(0) width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float # Create data folders create_dir(unknown_folder) create_dir(logs_folder) # Create logs file log_file = logs_folder + "/" + datetime.datetime.now().strftime("%d-%m-%Y") + ".csv" fps = FPS().start() nb_frames = 0 last_bbs = [] while(True): ret, frame = cap.read() face_detection_result = face_detector.detect_frame(frame) pil_im = Img.fromarray(frame) if nb_frames % 5 == 0: persons, detected_persons = arcface_classifier.recognize_person(pil_im, face_detection_result) frame = np.array(pil_im) for i, person in zip(range(len(persons)), persons): # Si début du programme ou personne supplémentaire ou en moins, réinitialisation if not last_bbs or len(last_bbs) != len(persons): last_bbs = [(0,0,0,0) for x in range(len(persons))] label, x1, y1, x2, y2, confidence = str(person[0]), int(person[1]), int(person[2]), int(person[3]), int(person[4]), float(person[5]) data_to_save = [datetime.datetime.now().strftime("%d-%m-%Y"), datetime.datetime.now().strftime("%H:%M:%S"), label, "{:.2f}".format(confidence)] if label == 'Unknown': if get_distance(last_bbs[i], (x1,y1,x2,y2)) != -1 and valid_photo(x1, y1, x2, y2, width, height): try: id_unknown = uuid.uuid4() to_crop = np.array(pil_im) cropped = to_crop[y1-MARGIN:y2+MARGIN, x1-MARGIN:x2+MARGIN] #0.5*... à tester ! cv2.imwrite(f"{unknown_folder}/{id_unknown}.jpg", cropped) data_to_save.append(id_unknown) append_list_as_row(log_file, data_to_save) except: pass color = (0,0,255) #red color else: if get_distance(last_bbs[i], (x1,y1,x2,y2)) != -1 and valid_photo(x1, y1, x2, y2, width, height): append_list_as_row(log_file, data_to_save) color = (0,255,0) #green color cv2.putText(frame, f"{label} - {confidence:.2f}", (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2, ) cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 2) if get_distance(last_bbs[i], (x1,y1,x2,y2)) != -1: last_bbs[i] = (x1, y1, x2, y2) cv2.imshow('frame', frame) nb_frames += 1 if nb_frames == 101: nb_frames = 0 fps.update() if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() cap.release() cv2.destroyAllWindows() print("[INFO] Elasped time {:.2f}".format(fps.elapsed())) print("[INFO] Approx. FPS {:.2f}".format(fps.fps())) return(0)
# saving the new compressed frame cv2.imwrite("saved_frames/after/frame%d.jpg" % count, frame_bg, [int(cv2.IMWRITE_JPEG_QUALITY), 50]) count += 1 # 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_bg, (startX, startY), (endX, endY), (0, 0, 200), 1) # cv2.putText(frame_bg, text, (startX, y), # cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 1) info = [("FPS", "{:.2f}".format(fps.fps()))] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame_bg, text, (10, h - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) if (frame_bg.size): pass else: frame_bg = frame # show the output frame cv2.imshow("Frame", frame_bg) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop
def main(self): q = queue.Queue() def frame_render(queue_from_cam): frame = self.cap.read() frame = cv2.resize(frame, (self.width, self.height)) queue_from_cam.put(frame) cam = threading.Thread(target=frame_render, args=(q, )) cam.start() cam.join() frame = q.get() q.task_done() fps = FPS().start() try: img, orig_im, dim = prep_image(frame, self.inp_dim) im_dim = torch.FloatTensor(dim).repeat(1, 2) if self.CUDA: #### If you have a gpu properly installed then it will run on the gpu im_dim = im_dim.cuda() img = img.cuda() # with torch.no_grad(): #### Set the model in the evaluation mode output = self.model(Variable(img), self.CUDA) output = write_results( output, self.confidence, self.num_classes, nms=True, nms_conf=self.nms_thesh) #### Localize the objects in a frame output = output.type(torch.half) if list(output.size()) == [1, 86]: pass else: output[:, 1:5] = torch.clamp(output[:, 1:5], 0.0, float(self.inp_dim)) / self.inp_dim # im_dim = im_dim.repeat(output.size(0), 1) output[:, [1, 3]] *= frame.shape[1] output[:, [2, 4]] *= frame.shape[0] list( map(lambda x: write(x, frame, self.classes, self.colors), output)) # cv2.imshow("Object Detection Window", frame) x, y, w, h = b_boxes["bbox"][0], b_boxes["bbox"][1], b_boxes[ "bbox"][2], b_boxes["bbox"][3] distance = (2 * 3.14 * 180) / ( w + h * 360) * 1000 + 3 ### Distance measuring in Inch feedback = ("{}".format(labels["Current Object"]) + " " + "is" + " at {} ".format(round(distance)) + "Inches") speak.Speak(feedback) print(feedback) except: pass fps.update() fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.1f}".format(fps.fps())) ret, jpeg = cv2.imencode('.jpg', frame) return jpeg.tostring()
def startFaceDetection(self): # setup camera to capture video self.video_capture = cv2.VideoCapture(self.camid) # start frames capturing timer fps = FPS().start() # start indefinite loop for video capturing while True: # fetch camera frame ret, frame = self.video_capture.read() frame1 = frame.copy() # validate if image is captured, else stop video capturing if ret!=True: print("\n[INFO] Camera not detected") self.video_capture.release() cv2.destroyAllWindows() return # fetch face location face_locations = self.face.detectFace(frame) # fetch user details face_encodings, face_landmarks_list, users = self.face.getFaceID(frame1, face_locations) count=0 # loop through face encodings and face boundary boxes for (top, right, bottom, left), user, face_encoding, face_landmarks in zip(face_locations, users, face_encodings, face_landmarks_list): # Draw a box around the face cv2.rectangle(frame, (left, top), (right, bottom), (255, 0, 0), 2) # Draw a label with a name below the face cv2.rectangle(frame, (left, bottom), (right, bottom+20), (255, 0, 0), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX if user[0]!='Unknown': # put label for confidence cv2.rectangle(frame, (left, top-20), (right, top), (255, 0, 0), cv2.FILLED) cv2.putText(frame, str(round(user[1]*100,2))+'%', (left + 6, top-3), font, 0.5, (255, 255, 255), 1) cv2.putText(frame, user[0], (left + 6, bottom+15), font, 0.5, (255, 255, 255), 1) else: print("[INFO] Loading secondary model") # make prediction using secondary model # recognize face user = self.face1.getFaceID(frame1, top, right, bottom, left, face_encoding, face_landmarks)[0] if user[0] == 'Unknown': print("[INFO] Face not available in secondary model") cv2.destroyAllWindows() # train the unknown face # assign user a temporary ID #userID = input("[INPUT REQUIRED] Please enter user id: ") userID = str(2000+len(os.listdir(self.face1.directory))) # take pictures of user and verify he is not recognized flag, user, images = self.verifyUser() # if user is not recognized then train the user if flag==False: print("[INFO] Please wait while your face is being trained...") self.face1.savePictures(images, userID) self.face1.trainFace() print("[INFO] Training Completed. Starting Recognition process...") # recognize again user = self.face1.getFaceID(frame1, top, right, bottom, left, face_encoding, face_landmarks)[0] if user[0] == 'Unknown': continue cv2.putText(frame, user[0], (left + 6, bottom+15), font, 0.5, (255, 255, 255), 1) cv2.rectangle(frame, (left, top-20), (right, top), (255, 0, 0), cv2.FILLED) cv2.putText(frame, str(round(user[1]*100,2))+'%', (left + 6, top-3), font, 0.5, (255, 255, 255), 1) count+=1 # Display the resulting image cv2.imshow('Face Recognition', frame) # Hit 'q' on the keyboard to quit! if cv2.waitKey(1) & 0xFF == ord('q'): break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # Release handle to the webcam self.video_capture.release() cv2.destroyAllWindows()
confidence = detections[0, 0, i, 2] # filter out weak detections if confidence > args["confidence"]: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object 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") # draw the prediction on the frame 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) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break out.write(frame) fps.update() fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) vs.stop() cv2.destroyAllWindows()
def recognize(self, recognizer_path, le_path, user, confidence_limit=0.5): score = 0 res = {} start_time = time.time() # load our serialized face detector from disk print("[INFO] loading face detector...") protoPath = os.path.sep.join([self.detector, "deploy.prototxt"]) modelPath = os.path.sep.join( [self.detector, "res10_300x300_ssd_iter_140000.caffemodel"]) detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath) # load our serialized face embedding model from disk print("[INFO] loading face recognizer...") embedder = cv2.dnn.readNetFromTorch(self.embedding_model) # load the actual face recognition model along with the label encoder recognizer = pickle.loads(open(recognizer_path, "rb").read()) le = pickle.loads(open(le_path, "rb").read()) # initialize the video stream, then allow the camera sensor to warm up print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) # start the FPS throughput estimator fps = FPS().start() # loop over frames from the video file stream while True: # grab the frame from the threaded video stream frame = vs.read() # resize the frame to have a width of 600 pixels (while # maintaining the aspect ratio), and then grab the image # dimensions frame = imutils.resize(frame, width=600) (h, w) = frame.shape[:2] # construct a blob from the image imageBlob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) # apply OpenCV's deep learning-based face detector to localize # faces in the input image detector.setInput(imageBlob) detections = detector.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 if confidence > confidence_limit: # compute the (x, y)-coordinates of the bounding box for # the face box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # extract the face ROI face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] # ensure the face width and height are sufficiently large if fW < 20 or fH < 20: continue # construct a blob for the face ROI, then pass the blob # through our face embedding model to obtain the 128-d # quantification of the face faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) embedder.setInput(faceBlob) vec = embedder.forward() # perform classification to recognize the face preds = recognizer.predict_proba(vec)[0] j = np.argmax(preds) proba = preds[j] name = le.classes_[j] if name == user and proba > 0.7: score += 1 # draw the bounding box of the face along with the # associated probability limit = min([10, len(name)]) text = "{}: {:.2f}%".format(name[:limit], proba * 100) # print(text) 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) # update the FPS counter fps.update() # 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"): res["success"] = False break if score > 20: res["success"] = True break if time.time() - start_time > 20.0: res["success"] = False break # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop() return res
class DroidVisionThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.running = True self.fps_counter = FPS().start() self.camera = PiVideoStream(resolution=(config.RAW_FRAME_WIDTH, config.RAW_FRAME_HEIGHT)) self.camera.start() time.sleep(config.CAMERA_WARMUP_TIME) # wait for camera to initialise self.frame = None self.frame_chroma = None self.centre = config.CENTRE self.can_see_lines = False self.last_yellow_mean = config.WIDTH * 0.8 self.last_blue_mean = config.WIDTH * 0.2 self.desired_steering = config.NEUTRAL_STEERING # 0 = left, 0.5 = center, 1 = right self.desired_throttle = config.NEUTRAL_THROTTLE # 0 = stop, 0.5 = medium speed, 1 = fastest def run(self): debug("DroidVisionThread: Thread started") self.vision_processing() self.camera.stop() cv2.destroyAllWindows() debug("DroidVisionThread: Thread stopped") def stop(self): self.running = False debug("DroidVisionThread: Stopping thread") def vision_processing(self): while self.running: self.grab_frame() # colour mask blue_mask = cv2.inRange(self.frame_chroma, config.BLUE_CHROMA_LOW, config.BLUE_CHROMA_HIGH) yellow_mask = cv2.inRange(self.frame_chroma, config.YELLOW_CHROMA_LOW, config.YELLOW_CHROMA_HIGH) colour_mask = cv2.bitwise_or(blue_mask, yellow_mask) colour_mask = cv2.erode(colour_mask, config.ERODE_KERNEL) colour_mask = cv2.dilate(colour_mask, config.DILATE_KERNEL) # lines lines = cv2.HoughLinesP(colour_mask, config.HOUGH_LIN_RES, config.HOUGH_ROT_RES, config.HOUGH_VOTES, config.HOUGH_MIN_LEN, config.HOUGH_MAX_GAP) blue_lines = np.array([]) yellow_lines = np.array([]) if lines != None: for line in lines: x1,y1,x2,y2 = line[0] angle = np.rad2deg(np.arctan2(y2-y1, x2-x1)) if config.MIN_LINE_ANGLE < abs(angle) < config.MAX_LINE_ANGLE: if config.IMSHOW: cv2.line(self.frame, (x1,y1), (x2,y2), (0,0,255), 1) if angle > 0: yellow_lines = np.append(yellow_lines, [x1, x2]) elif angle < 0: blue_lines = np.append(blue_lines, [x1, x2]) # find centre blue_mean = self.last_blue_mean yellow_mean = self.last_yellow_mean if len(blue_lines): blue_mean = int(np.mean(blue_lines)) if len(yellow_lines): yellow_mean = int(np.mean(yellow_lines)) self.centre = (blue_mean + yellow_mean) / 2.0 self.last_blue_mean = blue_mean self.last_yellow_mean = yellow_mean self.can_see_lines = (len(blue_lines) or len(yellow_lines)) # set steering and throttle self.desired_steering = (1.0 - (self.centre / config.WIDTH)) if len(blue_lines) or len(yellow_lines): self.desired_throttle = 0.22 else: self.desired_throttle = 0 if config.IMSHOW: cv2.circle(self.frame, (int(self.centre), config.HEIGHT - 20), 10, (0,0,255), -1) cv2.imshow("colour_mask without noise", colour_mask) cv2.imshow("raw frame", self.frame) cv2.waitKey(1) def grab_frame(self): self.fps_counter.update() # grab latest frame and index the ROI self.frame = self.camera.read() self.frame = self.frame[config.ROI_YMIN:config.ROI_YMAX, config.ROI_XMIN:config.ROI_XMAX] # convert to chromaticity colourspace B = self.frame[:, :, 0].astype(np.uint16) G = self.frame[:, :, 1].astype(np.uint16) R = self.frame[:, :, 2].astype(np.uint16) Y = 255.0 / (B + G + R) b = (B * Y).astype(np.uint8) g = (G * Y).astype(np.uint8) r = (R * Y).astype(np.uint8) self.frame_chroma = cv2.merge((b,g,r)) def get_fps(self): self.fps_counter.stop() return self.fps_counter.fps() def get_error(self): return (config.CENTRE - self.centre) def get_steering_throttle(self): return self.desired_steering, self.desired_throttle
# Draw "line_center" frame = cv2.rectangle(frame, (line_center, vh), (line_center + line_thickness, 0), (0, 0, 0), -1) # Calculate, Generate and Draw "Total Fish" text counter_text = 'Total Fish: {}'.format(counter) counter_text_size, counter_text_baseline = cv2.getTextSize( counter_text, cv2.FONT_HERSHEY_PLAIN, 1, 1) frame = cv2.rectangle(frame, (5, 0), (counter_text_size[0] + 5, 20), (0, 0, 255), -1) frame = cv2.putText(frame, counter_text, (5, 15), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 1) # Calculate, Generate and Draw "FPS" text fps.update() fps.stop() fps_text = 'FPS: {}'.format(round(fps.fps(), 2)) fps_text_size, fps_text_baseline = cv2.getTextSize(fps_text, cv2.FONT_HERSHEY_PLAIN, 1, 1) frame = cv2.putText(frame, fps_text, (vw - fps_text_size[0], 15), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 0), 1) # Display the final combine of the orignal frame including tracked item cv2.imshow('frame', frame) # cv2.imshow('mask', fMask) # Display frame is being refresh every 30ms, change to 0 if manual forward required key = cv2.waitKey(30) # should "Q" is press, stop loop if key == ord('q'): break videWriter.write(frame) # Write frame to video output
cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame and update # the FPS counter rawCapture.truncate(0) fps.update() # check to see if the desired number of frames have been reached if i == args["num_frames"]: break # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() stream.close() rawCapture.close() camera.close() # created a *threaded *video stream, allow the camera sensor to warmup, # and start the FPS counter print("[INFO] sampling THREADED frames from `picamera` module...") vs = PiVideoStream().start() time.sleep(2.0) fps = FPS().start() # loop over some frames...this time using the threaded stream
cv2.putText(frame,"vehicle detected",(20,250),font,1,(0,255,0),2) cv2.putText(frame,str(vehicle),(5,250),font,1,(0,255,0),2) GPIO.output(led,True) else: GPIO.output(led,False) else: GPIO.output(led,False) cv2.imshow("Frame", frame) fps.update() key = cv2.waitKey(1) & 0xFF if key == ord("q"): GPIO.cleanup() fps.stop() print("############### appx fps {:.2f}".format(fps.fps())) break camera.stop() cv2.destroyAllWindows()