def live_lec_view(self): fps = FPS().start() data = "" payload_size = struct.calcsize("L") while True: while len(data)<payload_size: data+=self.s.recv(4096) packed_msg_size = data[:payload_size:] data = data[payload_size:] msg_size = struct.unpack("L", packed_msg_size)[0] while len(data)<msg_size: data+=self.s.recv(4096) frame_data = data[:msg_size] data = data[msg_size:] frame = pickle.loads(frame_data) cv2.imshow('Live Lecture - (' + self.lec_host[0] + ' ' + self.lec_host[1] + ')', frame) key = cv2.waitKey(1) & 0xFF == ord('q') if key: self.s.close() break fps.update() cv2.destroyAllWindows()
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 update(self): # keep looping infinitely until the thread is stopped print("[INFO] Waiting for Feed") fps = FPS().start() for f in self.stream: #print("[INFO] Reading Feed") self.frameCaptured = True # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array fps.update() self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() fps.stop() #print("Thread FPS: {: .2f}".format(fps.fps())) return
def track(self): CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] net = cv2.dnn.readNetFromCaffe(self.p, self.m) vs = VideoStream(src=self.i).start() writer = None W = None H = None ct = CentroidTracker(maxDisappeared=40, maxDistance=50) ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} totalFrames = 0 totalDown = 0 totalUp = 0 fps = FPS().start() while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream frame = vs.read() #frame = frame[1] if get(self.i, 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 self.i 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 self.o is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(self.o, 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 % self.s == 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 > self.c: # 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, 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 (self.objectID, centroid) in objects.items(): # check to see if a trackable object exists for the current # object ID to = trackableObjects.get(self.objectID, None) # if there is no existing trackable object, create one if to is None: to = TrackableObject(self.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[self.objectID] = to # draw both the ID of the object and the centroid of the # object on the output frame text = "ID {}".format(self.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, (0, 255, 0), -1) # construct a tuple of information we will be displaying on the # frame '''info = [ ("Up", totalUp), ("Down", totalDown), ("Status", status), ] # loop over the info tuples and draw them on our frame for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)''' # check to see if we 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 get(self.i, False): # vs.stop() # otherwise, release the video file pointer else: vs.release() # close any open windows cv2.destroyAllWindows()
for (i, (k, v)) in enumerate(info): # Draws each info on screen text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) 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:
def connect(self, video_url=None): if video_url is None: capture = cv2.VideoCapture(self.connect_str) ended = True else: capture = cv2.VideoCapture(video_url) ended = capture.isOpened() ct_list = {} rects = {} for cl in self.obj_detection_rules.all(): ct_list[cl.class_name] = CentroidTracker( maxDisappeared=cl.max_id_live, maxDistance=cl.max_id_distance) rects[cl.class_name] = [] trackers = [] trackableObjects = {} totalFrames = 0 fps = FPS().start() res, init_frame = capture.read() if init_frame is None: comment = "{} - No frame".format(self.connect_str) logger.error(comment) self.send_to_report(comment) cv2.destroyAllWindows() return else: width = capture.get(cv2.CAP_PROP_FRAME_WIDTH) # float height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT) # float # cv2.imshow("frame", init_frame) event_list = VisitEvent.objects.all() for e in event_list: e.delete() clients = self.clients.all() for cl in clients: cl.login() # for rule in self.aktive_rate_rules.filter(id=9): # rule.draw_zones(init_frame.copy()) while (ended): ret, frame = capture.read() if (cv2.waitKey(1) & 0xFF == ord('q')) or (type(frame) == type(None)): cv2.destroyAllWindows() break for cl in self.obj_detection_rules.all(): rects[cl.class_name] = [] # мы производим поиск новых объектов только раз в Х кадров # (чуть реже раза в две секунды по 25 или 6 секунд в 10) if totalFrames % self.skip_frames == 1: print("new detectings {}".format(self)) trackers = [] image = Image.fromarray(frame) boxs, classes = yolo.detect_image(image, self.search_classes) for box, class_name in zip(boxs, classes): min_size = self.obj_detection_rules.filter( class_name=class_name).get().min_size (x, y, w, h) = [int(v) for v in box] if w >= min_size or h >= min_size: """ Use CSRT (cv2.TrackerCSRT_create) when you need higher object tracking accuracy and can tolerate slower FPS throughput Use KCF (cv2.TrackerKCF_create) when you need faster FPS throughput but can handle slightly lower object tracking accuracy Use MOSSE (cv2.TrackerMOSSE_create) when you need pure speed """ tracker = cv2.TrackerKCF_create() tracker.init(frame, (x, y, w, h)) trackers.append((tracker, class_name)) print("found {}".format(len(boxs))) # loop over the trackers for tracker, class_name in trackers: # update the tracker and grab the updated position ret, bbox = tracker.update(frame) # unpack the position object startX = int(bbox[0]) startY = int(bbox[1]) endX = int(bbox[0] + bbox[2]) endY = int(bbox[1] + bbox[3]) # add the bounding box coordinates to the rectangles list rects[class_name].append((startX, startY, endX, endY)) # Для показа расскоментить cv2.rectangle(frame, (startX, startY), (endX, endY), (255, 0, 0), 2, 1) objects = [] disappered_objects = [] for class_name in self.search_classes: tmp_objects = ct_list[class_name].update(rects[class_name]) objects = objects + [("{}_{}".format(class_name, obj_id), data, class_name) for obj_id, data in tmp_objects.items()] disappered_objects = list( set(disappered_objects) | set(ct_list[class_name].near_disappeared())) for (objectID, data, cl_name) in objects: # check to see if a trackable object exists for the current object ID centroid = data[0] # (cX, cY) rect = data[1] # [startX, startY, endX, endY] to = trackableObjects.get(objectID, None) # if there is no existing trackable object, create one if to is None: to = TrackableObject(objectID, centroid, cl_name, rect) else: to.current_centroid = centroid to.current_rect = rect trackableObjects[objectID] = to # для кадра в котором осуществлялась детекция кроме самих объектов # еще и определить расстояние, сохранить в отслеживаемых объектах to.distance_to_cam, to.l, to.b = self.get_distance( centroid[0], centroid[1]) trackableObjects[objectID] = to for rule in self.aktive_rate_rules.filter(is_group_rule=False): for obj in trackableObjects.values(): if obj.int_id in disappered_objects: rule.activate_event(obj, frame.copy(), self, disappered=True) else: rule.activate_event(obj, frame.copy(), self) # for obj in disappered_objects: # for o in trackableObjects.values(): # if o.int_id == obj: # trackableObjects.pop(o) for rule in self.aktive_rate_rules.filter(is_group_rule=True): active_objects = [ obj for obj in list(trackableObjects.values()) if obj.int_id not in disappered_objects ] rule.activate_group_event(active_objects, frame.copy(), self) # increment the total number of frames processed thus far and # then update the FPS counter totalFrames += 1 fps.update() fps_count = capture.get(cv2.CAP_PROP_FPS) comment = "Total frames = {} fps = {}".format( totalFrames, fps_count) print(comment) if (fps_count < 10): comment = "Слишком низкий ФПС: " + comment self.send_to_report(comment) # Для показа расскоментить cv2.imshow(self.ip, frame) if video_url is not None: ended = capture.isOpened() self.send_to_report("Обрыв коннекта к камере")
class camera (threading.Thread): def __init__(self, camid,camname,camlocation,cam_type,threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter # Getting camera details self.camname = camname self.camid = camid self.camlocation = camlocation cam_type = cam_type self.face_cascade = cv2.CascadeClassifier('./models/haarcascades/haarcascade_frontalface_default.xml') self.font= cv2.FONT_HERSHEY_SIMPLEX self.fileDir = os.path.dirname(os.path.realpath(__file__)) self.unknownusers_dir = os.path.join(self.fileDir, "img/unknownusers") self.classifierModel = os.path.join(self.fileDir, 'ginilib/openface/generated-embeddings/classifier.pkl') if os.path.exists(self.classifierModel): with open(self.classifierModel, 'rb') as f: if sys.version_info[0] < 3: (self.le, self.clf) = pickle.load(f) else: (self.le, self.clf) = pickle.load(f, encoding='latin1') dbs=dbops() # create a directory for camera settingdetails = dbs.get_settingsdetails() rootpathprimary = settingdetails[0][2] rootpathsecondary = settingdetails[0][3] rootpathtertiary = settingdetails[0][4] if os.path.exists(rootpathprimary): pvideoroot = rootpathprimary else: self.createdir(rootpathprimary) pvideoroot = rootpathprimary if os.path.exists(rootpathsecondary): svideoroot = rootpathsecondary else: self.createdir(rootpathsecondary) svideoroot = rootpathsecondary if os.path.exists(rootpathtertiary): tvideoroot = rootpathtertiary else: self.createdir(rootpathtertiary) tvideoroot = rootpathtertiary camdir = pvideoroot+"/videos/" + camname self.base = pvideoroot+"/videos/" self.root = self.base + camname + "/live" self.root_hist = self.base + camname + "/hist" self.createdir(camdir) self.createdir(self.base) self.createdir(self.root) self.createdir(self.root_hist) framerate = 18.0 self.fps = FPS().start() self.Camtype=cam_type if self.Camtype=="webcam": try: ur =int(self.camid) self.video = WebcamVideoStream(src=ur).start() except Exception as e: print(e) print("Unable to open",camname) elif self.Camtype == "rtsp": try: ur = self.camid self.video = VideoStream(ur).start() except Exception as e: # print(e) print("Unable to open", camname) elif self.Camtype=="http": try: self.ur = camid self.imgResp = urllib.request(self.ur) # self.imgResp= requests.get(self.ur,stream=True,verify=True) except Exception as e: #print(e) print("Unable to open",camname) elif self.Camtype=="pi": try: # camera = PiCamera() # rawCapture = PiRGBArray(camera) # allow the camera to warmup time.sleep(0.1) except Exception as e: #print(e) print("Unable to open",camname) def run(self): print ("Starting " + self.name) #threadLock.acquire() dbs=dbops() counter = 0 self.trids, self.trname, self.trencodes = dbs.get_Track_user_list() self.yr = str(date.today().strftime("%Y")) self.mn = str(date.today().strftime("%m")) self.dy = str(date.today().strftime("%d")) self.FYear = self.root_hist + "/" + self.yr self.monpath = self.FYear + "/" + self.mn self.dypath = self.monpath + "/" + self.dy self.createdir(self.FYear) self.createdir(self.monpath) self.createdir(self.dypath) self.start = datetime.now().strftime("%Y%m%d%H%M%S") self.delta = timedelta(minutes=10) # - configuration part self.end = datetime.strptime(self.start, "%Y%m%d%H%M%S") + self.delta self.filename = self.dypath + "/" + self.start + ".avi" # Define the codec and create VideoWriter object self.fourcc = cv2.VideoWriter_fourcc("M", "J", "P", "G") self.out = cv2.VideoWriter(self.filename, self.fourcc, 26.0, (320, 240)) #0x00000021 while (True): img=None try: if self.Camtype=="webcam": try: img = self.video.read() except: print("Unable to read " + str(self.camname)) elif self.Camtype == "rtsp": try: img = self.video.read() except: print("Unable to read " + str(self.camname)) elif self.Camtype=="http": try: imgResp = urllib.urlopen(self.ur) imgNp = np.array(bytearray(imgResp.read()),dtype=np.uint8) img = cv2.imdecode(imgNp,-1) except: print("Unable to read "+ str(self.camname)) elif self.Camtype=="pi": #camera.capture(rawCapture, format="bgr") #img = rawCapture.array time.sleep(0.1) # put the image on screen cv2.waitKey(100) if np.all(img == None): print("Image value is empty for : " + self.camname) else: #img = self.facerec(img) # Do the face prediction img=self.Track_user(img) self.fps.update() width = 800 height = 600 img = cv2.resize(img, (width, height)) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.putText(img, "TENSOR LABS - BANGALORE", (int(width / 3.5), 60), self.font, 0.8, (0, 255, 255),thickness=2) datefield = "Date :" + str(date.today().strftime("%d-%m-%Y")) cv2.putText(img, datefield, (width - 180, height-40), self.font, 0.5, (255, 255, 255),thickness=2) cv2.putText(img, self.camname , ((width - 120), 60), self.font, 0.8,(0, 0, 255),thickness=2) # self.out.write(img) # cv2.imshow(camname, img) #Save Live Image saveto = self.root +"/live"+ str(counter) + ".jpg" if counter >= 3: counter = 0 else: counter += 1 #create directory if not exist cv2.imwrite(saveto, img) print(saveto) # Save backup frame = cv2.resize(img, (320, 240)) tod=datetime.now().strftime("%Y%m%d%H%M%S") tod=datetime.strptime(tod, "%Y%m%d%H%M%S") if tod >= self.end: # Reset the directory path self.yr = str(date.today().strftime("%Y")) self.mn = str(date.today().strftime("%m")) self.dy = str(date.today().strftime("%d")) self.FYear = self.root_hist + "/" + self.yr self.monpath = self.FYear + "/" + self.mn self.dypath = self.monpath + "/" + self.dy self.createdir(self.FYear) self.createdir(self.monpath) self.createdir(self.dypath) self.start = datetime.now().strftime("%Y%m%d%H%M%S") self.end = datetime.strptime(self.start, "%Y%m%d%H%M%S") + self.delta self.filename = self.dypath + "/" +self.start + ".avi" print ("creating file") print(self.filename) self.out.write(frame) self.out.release() self.out = cv2.VideoWriter(self.filename, self.fourcc, 26.0, (320, 240)) #0x00000021 else: self.out.write(frame) #Save Backup ends here key = cv2.waitKey(1) & 0xFF if key == ord("q"): break self.fps.stop() except ThreadError: print(ThreadError) self.thread_cancelled = True print("exiting") def facerec(self,img): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Show faces faces = self.face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(30, 30)) for (x, y, w, h) in faces: #id = -1 cv2.rectangle(img, (x, y), (x + w + 10, y + h + 10), (0, 255, 255),thickness=3) norm_image2 = img[y:y + h, x:x + w] norm_image2 = cv2.resize(norm_image2, (96, 96)) rgb = norm_image2[:, :, ::-1] facelocations = face_recognition.face_locations(rgb, model="cnn") #print(facelocations) if facelocations: faceencodes = face_recognition.face_encodings(rgb, facelocations) predictions = self.clf.predict_proba(faceencodes).ravel() maxI = np.argmax(predictions) maxvalue=np.max(predictions) print(predictions) print(maxvalue) print(maxI) id = self.le.inverse_transform(maxI) confidence = predictions[maxI] if confidence < 0.95 : id = -1 if maxvalue <= 0.95: id = -1 else: id = -1 confidence = 0 print(id) if id >= 1: content = dbs.get_user_details(str(id)) if content: cv2.putText(img, "ID:"+str(id), (x , y - 30), self.font, 0.4, (0, 0, 255),) txt = content[0].upper() #+ "(" + content[2].upper() + ")" cv2.putText(img, txt, (x, y-10), self.font, 0.4, (0, 0, 255)) #cv2.putText(img, str(content[1]), (x + w + 10, y + 20), self.font, 0.4, (0, 0, 255)) dbs.Add_log(id) if id in 19215626: message = [ 'Honourable Minister', 'Mr. Ravishankar Prasad '] #for msg in message: # self.wish(msg) else: msg="Hello" + content[0].upper() #self.wish(msg) id=-1 else: cv2.putText(img, "UNKNOWN", (x, y-10), self.font, 0.4, (0,0,255)) #cv2.putText(img, "Recording", (x + w + 12, y - 10), self.font, 0.4, 255) #userid = "U" + str(datetime.now().strftime("%d%H%M%S")) + ".jpg" #saveunknownto = os.path.join(self.unknownusers_dir, userid) face = img[y:y + h + 10, x:x + w + 10] face = cv2.resize(face, (138, 138)) #cv2.imwrite(saveunknownto, face) id = 0 return img def Track_user(self,img): dbs=dbops() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Show faces faces = self.face_cascade.detectMultiScale(gray, 1.3, 5, minSize=(30, 30)) for (x, y, w, h) in faces: #id = -1 img=cv2.rectangle(img, (x, y), (x + w + 10, y + h + 10), (0, 255, 255),thickness=3) norm_image2 = img[y:y + h, x:x + w] norm_image2 = cv2.resize(norm_image2, (96, 96)) rgb = norm_image2[:, :, ::-1] facelocations = face_recognition.face_locations(rgb, model="cnn") print(facelocations) if facelocations: faceencodes = face_recognition.face_encodings(rgb, facelocations) predictions = self.clf.predict_proba(faceencodes).ravel() maxI = np.argmax(predictions) maxvalue=np.max(predictions) print(predictions) print(maxvalue) print("maxI values are here",maxI) uid = self.le.inverse_transform(maxI) confidence = predictions[maxI] if confidence < 0.95 : uid = -1 if maxvalue <= 0.95: uid = -1 else: uid = -1 confidence = 0 print("uid is here",uid) #getting track id's from get_Transactions_id db ids=dbs.get_Transactions_id() print(ids) for r in ids: print(r) if uid==r[0]: print("cam name",self.camname) dbs.update_Transactions(uid,'Found','call',self.camname) return img def createdir(self,path): if not os.path.exists(path): try: os.makedirs(path) except: print ("unable to create directory", path)
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"]) net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) # 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('v4l2src device=/dev/video0 ! video/x-raw, format=YUY2, framerate=30/1, width=640, height=480 ! videoconvert ! appsink', cv2.CAP_GSTREAMER) vs = cv2.VideoCapture(0) # initialize the video writer (we'll instantiate later if need be) writer = None #gst_out2 = "appsrc ! video/x-raw ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay config-interval=1 ! udpsink host=127.0.0.1 port=10000" #writer2 =cv2.VideoWriter(gst_out2,cv2.CAP_GSTREAMER,0,float(20),(640,480),True) # 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=50, 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 total = 0 x = [] empty = [] empty1 = [] #cp_frame = np.zeros((480,640,3), dtype=np.uint8) # 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() start_time = time.time() #cp_frame = frame.copy() #frame = frame[1] if args.get("input", False) else frame # cv2.imshow("Real-Time Monitoring/Analysis W333dow", 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.00392, (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) total = len(objects) # 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) # define timestamp frames and logs 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", total), ("Status", status), ] # 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) # show the output frame cv2.imshow("Real-Time Monitoring/Analysis Window", frame) #if total >= 2: #writer2.write(cp_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())) # # 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 main(): parser = argparse.ArgumentParser( description='pix2pix checkpoint to SavedModel.') parser.add_argument('--size', dest='size', help='size of model', type=int, default=256) parser.add_argument('--cropSize', dest='cropSize', help='', type=int, default=1080) parser.add_argument('--sensor', dest='sensor', help='', type=int, default=0) parser.add_argument('--clientName', dest='clientName', help='', type=str, default='cc0') parser.add_argument('--serverIP', dest='serverIP', help='', type=str, default='10.42.0.1') parser.add_argument('--serverPORT', dest='serverPORT', help='', type=int, default=5550) parser.add_argument('--model', dest='model', help='', type=str, default='./model/pix2pixTF-TRT') parser.add_argument('--resize', dest='resize', help='', type=str2bool, const=True, default=False) parser.add_argument('--crop', dest='crop', help='', type=str2bool, const=True, default=False) parser.add_argument('--pix2pix', dest='pix2pix', help='', type=str2bool, const=True, default=False) args = parser.parse_args() print(args) if args.pix2pix: generator = tf.saved_model.load("./model/pix2pixTF-TRT") norm = (args.size / 2) - 0.5 vs = WebcamVideoStream(src=gstreamer_pipeline(sensor_id=0), device=cv2.CAP_GSTREAMER).start() client = imagiz.TCP_Client(server_ip=args.serverIP, server_port=args.serverPORT, client_name=args.clientName) encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90] fps = FPS().start() while True: try: image = vs.read() image_tf = tf.convert_to_tensor(image) if args.crop: image_tf = crop(image_tf, 0, 0, args.cropSize, args.cropSize) image = image_tf.numpy() if args.resize: image_tf = resize(image_tf, args.size) image = image_tf.numpy() if args.pix2pix: image = pix2pix(image_tf, args.size, norm, generator) # _, image = cv2.imencode('.jpg', image, encode_param) response = client.send(image) # print(response) fps.update() except Exception as e: print(e) vs.stop() break except KeyboardInterrupt: print("Keyboard stopped") vs.stop() break fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) vs.stop() print("exit main")
def Train(self): if not self.is_Train: print('load image_model ...') self.net = cv2.dnn.readNetFromCaffe(self.prorotxt_path, self.caffe_model_path) with tf.Session() as sess: sess.run(self.init) if self.load_model == True: print('load_model ...') ckpt = tf.train.get_checkpoint_state(path) saver.restore(sess, ckpt.model_checkpoint_path) for i in range(self.num_episodes): if not self.is_Train: CLASSES = ['bottle'] #["background", "aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", # "chair", "cow", "diningtable","dog", "horse", "motorbike", "person", "pottedplant", # "sheep","sofa", "train", "tvmonitor" self.col = -1 self.width = -1 self.row = -1 self.height = -1 self.frame = None self.frame2 = None self.inputmode = False self.rectangle = False self.trackWindow = None self.roi_hist = None self.roi = None self.cap = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() cv2.namedWindow('frame') cv2.setMouseCallback('frame', self.onMouse, param=(self.frame, self.frame2)) #termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) episode_buffer = experience_buffer() state = self.game.Reset() state = self.processState(state) dead = False reward_all = 0 epi = 0 while epi < self.max_epLength: epi += 1 if not self.is_Train: is_game_start = False self.frame = self.cap.read() #print(self.frame) self.frame = imutils.resize(self.frame, width=200, height=200) (h, w) = self.frame.shape[:2] blob = cv2.dnn.blobFromImage( cv2.resize(self.frame, (300, 300)), 0.007843, (300, 300), 127.5) self.net.setInput(blob) detections = self.net.forward() self.obstacle_points = [] for x in np.arange(0, detections.shape[2]): confidence = detections[0, 0, x, 2] if confidence > 0.2: ### set for changing idx = int(detections[0, 0, x, 1]) box = detections[0, 0, x, 3:7] * np.array( [w, h, w, h]) (startX, startY, endX, endY) = box.astype('int') label = "{}: {:.2f}%".format( 'obstacle', confidence * 100) cv2.rectangle(self.frame, (startX, startY), (endX, endY), self.obstacle_box_color, 2) self.obstacle_points.append({ 'row': startY, 'col': startX, 'row_size': endY - startY, 'col_size': endX - startX }) if self.trackWindow is not None: # hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) # dst = cv2.calcBackProject([hsv], [0], self.roi_hist, [0, 180], 1) ## Maybe this window is track size # ret, self.trackWindow = cv2.meanShift(dst, self.trackWindow, termination) # x, y, w, h = self.trackWindow # self.target_point = {'row' : int((2*y+h)/2), 'col' : int((2*x+w)/2)} # cv2.rectangle(self.frame, (x, y), (x+w, y+w), (0, 255, 0), 3) # is_game_start = True # else : # self.target_point = {'row' : -1, 'col' : -1} #in Sim m_row == -1 is_show = False ok, self.trackWindow = self.tracker.update( self.frame) if ok: x, y, w, h = self.trackWindow x, y, w, h = int(x), int(y), int(w), int(h) self.target_point = { 'row': int((2 * y + h) / 2), 'col': int((2 * x + w) / 2) } cv2.rectangle(self.frame, (x, y), (x + w, y + w), (0, 255, 0), 3) is_game_start = True else: cv2.putText(self.frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) self.target_point = {'row': -1, 'col': -1} show_frame = cv2.resize(self.frame, None, fx=2, fy=2) cv2.imshow('frame', show_frame) print(self.target_point) key = cv2.waitKey(60) & 0xFF if key == ord('i'): print('select target') self.inputmode = True self.frame2 = self.frame.copy() while self.inputmode: cv2.imshow('frame', self.frame) cv2.waitKey(0) fps.update() ### Idont know where it locatied if not is_game_start: epi -= 1 continue else: self.game.Update_ob_points(self.target_point, self.obstacle_points) if np.random.rand( 1 ) < self.e or self.total_steps < self.pre_train_steps: action = self.game.Get_action() else: action = sess.run( self.mainQN.predict, feed_dict={self.mainQN.scalarInput: [state]})[0] state_1, reward, dead = self.game.Step(action) state_1 = self.processState(state_1) self.total_steps += 1 episode_buffer.add( np.reshape( np.array([state, action, reward, state_1, dead]), [1, 5])) if self.total_steps > self.pre_train_steps: if self.e > self.endE: self.e -= self.stepDrop if self.total_steps % (self.update_freq) == 0: train_batch = self.myBuffer.sample(self.batch_size) Q_1 = sess.run(self.mainQN.predict, feed_dict={ self.mainQN.scalarInput: np.vstack(train_batch[:, 3]) }) Q_2 = sess.run(self.targetQN.Qout, feed_dict={ self.targetQN.scalarInput: np.vstack(train_batch[:, 3]) }) end_mutiplier = -(train_batch[:, 4] - 1) doubleQ = Q_2[range(self.batch_size), Q_1] targetQ = train_batch[:, 2] + (self.y * doubleQ * end_mutiplier) _ = sess.run(self.mainQN.updateModel, feed_dict={ self.mainQN.scalarInput: np.vstack(train_batch[:, 0]), self.mainQN.targetQ: targetQ, self.mainQN.actions: train_batch[:, 1] }) self.updateTarget(self.targetOps, sess) reward_all += reward state = state_1 if dead == True: break self.myBuffer.add(episode_buffer.buffer) self.jList.append(epi) self.rList.append(reward_all) f = open('./graph.txt', 'a') f.write( str(i) + '_th Game_End = Reward : ' + str(reward_all) + '/ Episode : ' + str(epi)) f.write('\n') f.close() self.game.Print_action_log() print( str(i) + '_th Game_End = Reward : ' + str(reward_all) + '/ Episode : ' + str(epi)) if i % 100 == 0: self.saver.save(sess, self.path + '/model-' + str(i) + '.ckpt') print('save model') if len(self.rList) % 10 == 0: print(self.rList) print(len(self.rList)) print(self.total_steps) print(np.mean(self.rList[-10:])) print(self.e) f_2 = open('./reward_mean.txt', 'a') f_2.write( str(i) + 'th : ' + str(np.mean(self.rList[-10:]))) f_2.write('\n') f_2.close() if not self.is_Train: cv2.destroyAllWindows() self.saver.save(sess, self.path + '/model-' + str(i) + '.ckpt') print("Percent of succesful episodes: " + str(sum(self.rList) / self.num_episodes) + "%") rMat = np.resize(np.array(self.rList), [len(self.rList) // 100, 100]) rMean = np.average(rMat, 1) plt.plot(rMean)
def hyperloop(): logger = Logger() logger.setLogLevel('info') logger.info('Started') state = State() for p in sys.argv: if (p == "--standalone" or p == "-s"): state.Standalone = True logger.info("Standalone mode activated") elif (p == "--nocamera" or p == "-n"): state.NoImageTransfer = True logger.info("Camera image transfer X11 disabled") elif (p == "--record" or p == "-r"): state.RecordImage = True logger.info("Record mode activated") elif (p == "--measure" or p == "-m"): state.MeasureMode = True logger.info("Measure mode activated") elif (p == "--invert" or p == "-i"): state.InvertCamera = True logger.info("Inverted camera activated") vs = PiVideoStream(resolution=(480, 368), framerate=32).start() piCamera = vs.camera piCamera.exposure_mode = 'sports' piCamera.ISO = 1600 camera = Camera(vs, not state.NoImageTransfer) # camera warmup camera.warmup() communication = Communication(logger) acceleration = Acceleration(logger) # reads any input incoming over UART / i2c / GPIO communication.readThreadStart() # measure acceleration acceleration.measureThreadStart() fps = FPS().start() # program loop while True: try: if ((not state.Stopped and state.Loaded) or state.Standalone): # if (state.StopSignalNum == 0 or (state.Approaching and not state.StopSignalNum == 0) or state.Standalone): # capture image from videostream and analyze camera.capture() fps.update() if (state.StopSignalNum == 0 and state.LapSignalCount < 2 and not state.Approaching == Signal.UPPER): communication.sendSpeedPercent(25) state.Approaching = Signal.UPPER logger.info("Approaching upper signal") # if we found our stop signal, announce it elif (state.StopSignalNum != 0 and not state.StopSignalAnnounced): communication.sendSpeedPercent(100) communication.buzzSignalNumber(state.StopSignalNum) state.setStopSignalAnnounced(True) state.Approaching = Signal.LAP logger.info("Approaching lap signal") # if we passed the lap signal twice, deccelerate to X percent elif (state.StopSignalAnnounced and state.LapSignalCount >= 2 and not state.Approaching == Signal.LOWER): communication.sendSpeedPercent(25) state.Approaching = Signal.LOWER logger.info("Approaching lower signal") elif (state.StopSignalAnnounced and state.StopSignalNum == state.CurrentNum and not state.ApproachStop): communication.sendApproachStop() communication.sendSpeedPercent(25) state.ApproachStop = True logger.info("Approaching stop") if (cv2.waitKey(1) & 0xFF) == ord('q'): break except KeyboardInterrupt: break except Exception as e: logger.logError(e) traceback.print_exc(limit=3, file=sys.stdout) fps.stop() communication.sendStartStop('stop') time.sleep(1) logger.info('FPS: ' + str(fps.fps())) logger.info('Aborting running threads') communication.readThreadStop() acceleration.measureThreadStop() logger.info('Stopped')
def main(): try: default_model_dir = 'all_models' default_model = 'ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite' default_labels = 'coco_labels.txt' parser = argparse.ArgumentParser() parser.add_argument('--model', help='.tflite model path', default=os.path.join(default_model_dir,default_model)) parser.add_argument('--labels', help='label file path', default=os.path.join(default_model_dir, default_labels)) parser.add_argument('--top_k', type=int, default=3, help='number of categories with highest score to display') parser.add_argument('--camera_idx', type=int, help='Index of which video source to use. ', default = 0) parser.add_argument('--threshold', type=float, default=0.1, help='classifier score threshold') args = parser.parse_args() #Initialize logging files logging.basicConfig(filename='storage/results.log', format='%(asctime)s-%(message)s', level=logging.DEBUG) print('Loading {} with {} labels.'.format(args.model, args.labels)) interpreter = common.make_interpreter(args.model) interpreter.allocate_tensors() labels = load_labels(args.labels) #vs = VideoStream(src=args.camera_idx, resolution=(2048, 1536)).start() #cap = vs.stream cap = cv2.VideoCapture(args.camera_idx) #cap = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER) time.sleep(2.0) # 5 MP #cap.set(3, 2048) #cap.set(4, 1536) # 4:3 resolutions # 640×480, 800×600, 960×720, 1024×768, 1280×960, 1400×1050, # 1440×1080 , 1600×1200, 1856×1392, 1920×1440, 2048×1536 # 2304×1728, 2560x1920, 2732×2048, 3200×2400, (3280, 2464) cap.set(3, 3200) cap.set(4, 2400) out = None fps = FPS().start() is_stopped = False current_fps = 4.0 visitations = Visitations() while cap.isOpened(): try: ret, frame = cap.read() if not ret: break if fps._numFrames < 500: fps.update() else: fps.stop() current_fps = fps.fps() logging.info("[INFO] elasped time: {:.2f}".format(fps.elapsed())) logging.info("[INFO] approx. FPS: {:.2f}".format(fps.fps())) fps = FPS().start() cv2_im = frame resized_frame = frame.copy() resized_frame = imutils.resize(frame, width=500) cv2_im_rgb = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2RGB) pil_im = Image.fromarray(resized_frame) common.set_input(interpreter, pil_im) interpreter.invoke() objs = get_output(interpreter, score_threshold=args.threshold, top_k=args.top_k) height, width, channels = cv2_im.shape visitations.update(objs, cv2_im, labels) cv2.namedWindow('Leroy',cv2.WINDOW_NORMAL) cv2.resizeWindow('Leroy', 800, 600) cv2.imshow('Leroy', cv2_im) except KeyboardInterrupt: print('Interrupted') try: sys.exit(0) except SystemExit: os._exit(0) except: logging.exception('Failed while looping.') if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() except: logging.exception('Failed on main program.')
def main(): fps = FPS().start() cap = cv2.VideoCapture(Input_Video) fourcc = cv2.VideoWriter_fourcc(*'XVID') writer = cv2.VideoWriter('../video/22.avi', fourcc, 10.0, (1920,1080)) YOLOINIT() f_num = 0 RED_cnt_1 = 0; BLUE_cnt_1 = 0 initBB_1 = None; tracker_1 = None RED_cnt_2 = 0; BLUE_cnt_2 = 0 initBB_2 = None; tracker_2 = None RED_cnt_3 = 0; BLUE_cnt_3 = 0 initBB_3 = None; tracker_3 = None RED_cnt_4 = 0; BLUE_cnt_4 = 0 initBB_4 = None; tracker_4 = None #hyper parameter vertices1 = [[[650, 300], [750, 300], [810, 210], [745, 210]]] #left-up / C29 vertices2 = [[[250, 780], [480, 800], [720, 340], [620, 340]]] #left-down / C28 vertices3 = [[[1140, 230], [1080, 230], [1150, 330], [1240, 330]]] #right-up / D29 vertices4 = [[[1270, 360], [1170, 360], [1450, 800], [1660, 800]]] #right-down / D28 pos = ['C29','C28','D29','D28'] r_up=0; r_down=0; l_up=0; l_down=0; while(cap.isOpened()): f_num =f_num +1 #print("F : ", f_num) (grabbed, frame) = cap.read() temp_r_1=RED_cnt_1; temp_b_1=BLUE_cnt_1 temp_r_2=RED_cnt_2; temp_b_2=BLUE_cnt_2 temp_r_3=RED_cnt_3; temp_b_3=BLUE_cnt_3 temp_r_4=RED_cnt_4; temp_b_4=BLUE_cnt_4 if f_num % 2== 0: blank_image = np.zeros((64, 1920, 3), np.uint8) frame[0:64, 0:1920] = blank_image park_cnt = [l_up,l_down,r_up,r_down] RED_cnt = [RED_cnt_1, RED_cnt_2, RED_cnt_3, RED_cnt_4] BLUE_cnt = [BLUE_cnt_1, BLUE_cnt_2, BLUE_cnt_3, BLUE_cnt_4] vertice = [vertices1, vertices2, vertices3, vertices4] Substracted = substraction(frame) layerOutputs, start, end = YOLO_Detect(frame) idxs, boxes, classIDs, confidences = YOLO_BOX_INFO(frame, layerOutputs, BaseConfidence, Base_threshold) #car detecting point 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) #car detecting bounding box Draw_Points(frame, Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h) #left up 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, vertices1) l_up, temp_r_1, temp_b_1 = park_count(park_cnt[0], temp_r_1, temp_b_1, RED_cnt_1, BLUE_cnt_1) #left down tracker_2, initBB_2, RED_cnt_2, BLUE_cnt_2 = Passing_Counter_Zone(Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h, initBB_2, frame, tracker_2, Substracted,\ RED_cnt_2, BLUE_cnt_2, vertices2) l_down, temp_r_2, temp_b_2 = park_count(park_cnt[1], temp_r_2, temp_b_2, RED_cnt_2, BLUE_cnt_2) #right up tracker_3, initBB_3, RED_cnt_3, BLUE_cnt_3 = Passing_Counter_Zone(Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h, initBB_3, frame, tracker_3, Substracted,\ RED_cnt_3, BLUE_cnt_3, vertices3) r_up, temp_r_2, temp_b_2 = park_count(park_cnt[2], temp_r_3, temp_b_3, RED_cnt_3, BLUE_cnt_3) #right down tracker_4, initBB_4, RED_cnt_4, BLUE_cnt_4 = Passing_Counter_Zone(Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h, initBB_4, frame, tracker_4, Substracted,\ RED_cnt_4, BLUE_cnt_4, vertices4) r_down, temp_r_2, temp_b_2 = park_count(park_cnt[3], temp_r_4, temp_b_4, RED_cnt_4, BLUE_cnt_4) #draw lines for i in range(0,4): draw_line(frame, vertice[i], RED_cnt[i], BLUE_cnt[i]) pts = detecting_zone(vertice[i]) cv2.polylines(frame, [pts], True, (0, 255, 0), 2) fps.update() fps.stop() #text at upper frame cv2.putText(frame, "FPS : " + "{:.2f}".format(fps.fps()), (50, 45), cv2.FONT_HERSHEY_SIMPLEX, 1.5,(200, 200, 200), 2) cv2.putText(frame, pos[0]+": {} / ".format(park_cnt[0])+pos[1]+": {} / ".format(park_cnt[1])+ pos[2]+": {} / ".format(park_cnt[2])+pos[3]+": {}".format(park_cnt[3]), (450, 45), cv2.FONT_HERSHEY_SIMPLEX, 1.5,(200, 200, 200), 2) cv2.putText(frame, "Frame : " + "{}".format(f_num), (1500, 45), cv2.FONT_HERSHEY_SIMPLEX, 1.5,(200, 200, 200), 2) #frame = cv2.resize(frame, (960, 540), interpolation=cv2.INTER_AREA) #1920, 1080 -> 1280,720 -> 960, 540 #Substracted = cv2.resize(Substracted , (1280, 720), interpolation=cv2.INTER_CUBIC) cv2.imshow("frame", frame) writer.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() writer.release() return
def evaluateallframescreatesubmission(frames, outputsubmissionfilepath, outputfile="./output_video1.mp4"): array_len = len(frames) #4931 frames for validation_0000 # 20, 200 frames in one file, downsample by 10 print("Frames lenth:", array_len) print("Final_array type:", type(frames)) # class 'list' objects = metrics_pb2.Objects() # submission objects frame_width = 1920 frame_height = 1280 out = cv2.VideoWriter(outputfile, cv2.VideoWriter_fourcc( 'M', 'P', '4', 'V'), 5, (frame_width, frame_height)) fps = FPS().start() wod_latency_submission.initialize_model() required_field = wod_latency_submission.DATA_FIELDS print(required_field) for frameid in range(array_len): #frameid = 5 print("frameid:", frameid) # {'key':key, 'context_name':context_name, 'framedict':framedict} currentframe=frames[frameid] convertedframesdict = convert_frame_to_dict_cameras(currentframe) #data_array[frameid] frame_timestamp_micros = convertedframesdict['TIMESTAMP']#['key'] context_name = currentframe.context.name #convertedframesdict['context_name'] #framedict = convertedframesdict['framedict'] # 10017090168044687777_6380_000_6400_000 #print('context_name:', context_name) # print('frame_timestamp_micros:', frame_timestamp_micros) # 1550083467346370 #result = wod_latency_submission.run_model(framedict[required_field[0]], framedict[required_field[1]]) #result = wod_latency_submission.run_model(**framedict) #Front_image = framedict[required_field[0]] start_time = time.time() #result = wod_latency_submission.run_model(Front_image) result = wod_latency_submission.run_model(**convertedframesdict)#All images end_time = time.time() elapsed_time = end_time - start_time print('Inference time: ' + str(elapsed_time) + 's') # print(result) createsubmisionobject(objects, result['boxes'], result['classes'], result['scores'], context_name, frame_timestamp_micros) # Save the original image #output_path = "./test.png" #visualization_util.save_image_array_as_png(Front_image, output_path) Front_image = convertedframesdict[required_field[0]] image_np_with_detections = Front_image.copy() visualization_util.visualize_boxes_and_labels_on_image_array(image_np_with_detections, result['boxes'], result['classes'], result['scores'], category_index, use_normalized_coordinates=False, max_boxes_to_draw=200, min_score_thresh=Threshold, agnostic_mode=False) display_str=f'Inference time: {str(elapsed_time*1000)}ms, context_name: {context_name}, timestamp_micros: {frame_timestamp_micros}' visualization_util.draw_text_on_image(image_np_with_detections, 0, 0, display_str, color='black') #visualization_util.save_image_array_as_png(image_np_with_detections, outputfile) name = './Test_data/frame' + str(frameid) + '.jpg' #print ('Creating\...' + name) # cv2.imwrite(name, image_np_with_detections) #write to image folder fps.update() #out.write(image_np_with_detections) out.write(cv2.cvtColor(image_np_with_detections, cv2.COLOR_RGB2BGR)) #cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # 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())) out.release() with open('objectsresult.pickle', 'wb') as f: pickle.dump(objects, f) submission = submission_pb2.Submission() submission.task = submission_pb2.Submission.DETECTION_2D submission.account_name = '*****@*****.**' submission.authors.append('Kaikai Liu') submission.affiliation = 'None' submission.unique_method_name = 'torchvisionfaster' submission.description = 'none' submission.method_link = "empty method" submission.sensor_type = submission_pb2.Submission.CAMERA_ALL submission.number_past_frames_exclude_current = 0 submission.number_future_frames_exclude_current = 0 submission.inference_results.CopyFrom(objects) f = open(outputsubmissionfilepath, 'wb') # output submission file f.write(submission.SerializeToString()) f.close() now = datetime.datetime.now() print("Finished validation, current date and time : ") print(now.strftime("%Y-%m-%d %H:%M:%S"))
def evaluateallframesgtfakesubmission(frames, outputsubmissionfilepath, outputfile="./output_video1.mp4"): array_len = len(frames) #4931 frames for validation_0000 # 20, 200 frames in one file, downsample by 10 print("Frames lenth:", array_len) print("Final_array type:", type(frames)) # class 'list' objects = metrics_pb2.Objects() # submission objects frame_width = 1920 frame_height = 1280 out = cv2.VideoWriter(outputfile, cv2.VideoWriter_fourcc( 'M', 'P', '4', 'V'), 5, (frame_width, frame_height)) fps = FPS().start() #wod_latency_submission.initialize_model() #required_field = wod_latency_submission.DATA_FIELDS #print(required_field) #Allconvertedframesdict=[] for frameid in range(array_len): #frameid = 5 print("frameid:", frameid) # {'key':key, 'context_name':context_name, 'framedict':framedict} frame=frames[frameid] convertedframesdict = convert_frame_to_dict_cameras(frame) #data_array[frameid] #Allconvertedframesdict.append(convertedframesdict) frame_timestamp_micros = convertedframesdict['TIMESTAMP']#['key'] context_name = frame.context.name o_list = [] boundingbox=[] boxscore=[] boxtype=[] for camera_labels in frame.camera_labels: if camera_labels.name != 1: #Only use front camera continue for gt_label in camera_labels.labels: o = metrics_pb2.Object() # The following 3 fields are used to uniquely identify a frame a prediction # is predicted at. o.context_name = frame.context.name # The frame timestamp for the prediction. See Frame::timestamp_micros in # dataset.proto. o.frame_timestamp_micros = frame.timestamp_micros # This is only needed for 2D detection or tracking tasks. # Set it to the camera name the prediction is for. o.camera_name = camera_labels.name # Populating box and score. box = label_pb2.Label.Box() box.center_x = gt_label.box.center_x box.center_y = gt_label.box.center_y box.length = gt_label.box.length box.width = gt_label.box.width boundingbox.append([box.center_x, box.center_y, box.length, box.width]) #width height o.object.box.CopyFrom(box) # This must be within [0.0, 1.0]. It is better to filter those boxes with # small scores to speed up metrics computation. o.score = 0.9 boxscore.append(o.score) # Use correct type. o.object.type = gt_label.type boxtype.append(o.object.type) o_list.append(o) print(f'Camera labelname: {camera_labels.name}, object type: { gt_label.type}, box:{box}') # Save the original image #output_path = "./test.png" #visualization_util.save_image_array_as_png(Front_image, output_path) boundingbox=np.array(boundingbox) boxscore=np.array(boxscore) boxtype=np.array(boxtype).astype(np.uint8) Front_image = convertedframesdict['FRONT_IMAGE'] image_np_with_detections = Front_image.copy() visualization_util.visualize_boxes_and_labels_on_image_array(image_np_with_detections, boundingbox, boxtype, boxscore, category_index, use_normalized_coordinates=False, max_boxes_to_draw=200, min_score_thresh=Threshold, agnostic_mode=False) display_str=f'context_name: {context_name}, timestamp_micros: {frame_timestamp_micros}' visualization_util.draw_text_on_image(image_np_with_detections, 0, 0, display_str, color='black') #visualization_util.save_image_array_as_png(image_np_with_detections, outputfile) name = './Test_data/frame' + str(frameid) + '.jpg' #print ('Creating\...' + name) # cv2.imwrite(name, image_np_with_detections) #write to image folder fps.update() #out.write(image_np_with_detections) out.write(cv2.cvtColor(image_np_with_detections, cv2.COLOR_RGB2BGR)) #cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # 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())) out.release() with open('objectsresult_gtvalall.pickle', 'wb') as f: pickle.dump(objects, f) # with open('allframedics.pickle', 'wb') as f: # pickle.dump(Allconvertedframesdict, f) submission = submission_pb2.Submission() submission.task = submission_pb2.Submission.DETECTION_2D submission.account_name = '*****@*****.**' submission.authors.append('Kaikai Liu') submission.affiliation = 'None' submission.unique_method_name = 'fake' submission.description = 'none' submission.method_link = "empty method" submission.sensor_type = submission_pb2.Submission.CAMERA_ALL submission.number_past_frames_exclude_current = 0 submission.number_future_frames_exclude_current = 0 submission.inference_results.CopyFrom(objects) f = open(outputsubmissionfilepath, 'wb') # output submission file f.write(submission.SerializeToString()) f.close() now = datetime.datetime.now() print("Finished validation, current date and time : ") print(now.strftime("%Y-%m-%d %H:%M:%S"))
def start_person_detection(): """ Определение объектов в видео потоке с помощь openCV, использован Dataset и информация с https://www.pyimagesearch.com/2017/09/18/real-time-object-detection-with-deep-learning-and-opencv/?utm_source=mybridge&utm_medium=blog&utm_campaign=read_more """ # Загрузка моделей print("Загрузка модели...") net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # Загрузка видеопотока print("Старт видеопотока...") vs = VideoStream(VIDEO).start( ) # В качестве аргумента можно использовать видеопоток от камеры time.sleep(2.0) fps = FPS().start() # Запуск видеопотока и разбиение на фреймы while True: # Чтение фреймов и разбиение на файлы frame = vs.read() frame = imutils.resize(frame, width=1200) # Преобразование фрейма в двоичный объект (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) 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 # Выход при ключе "q" if key == ord("q"): break # Обновление FPS fps.update() # Прекращение работы и вывод информации fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # Закрытие окна и прекращение рабоыт cv2.destroyAllWindows() vs.stop()
def _main_(args): # Set up device device = mvnc.Device(args['device']) device.open() fifoIn, fifoOut = graph.allocate_with_fifos(device, graphfile) # Configuration stuff needed for postprocessing the predictions config_path = args['config'] with open(config_path) as config_buffer: config = json.load(config_buffer) backend = config['model']['backend'] input_size = (config['model']['input_size_h'],config['model']['input_size_w']) labels = config['model']['labels'] max_box_per_image = config['model']['max_box_per_image'] anchors = config['model']['anchors'] gray_mode = config['model']['gray_mode'] nb_class = 1 # Predict bounding boxes if video_mode: cap = FileVideoStream(source).start() time.sleep(1.0) fps = FPS().start() fps_img = 0.0 counter = 0 while True: start = time.time() image = cap.read() # Preprocessing if depth == 1: # convert video to gray image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = cv2.resize(image, input_size, interpolation = cv2.INTER_CUBIC) image = np.expand_dims(image, 2) fimage = np.array(image, dtype=np.float32) else: image = cv2.resize(image, input_size, interpolation = cv2.INTER_CUBIC) fimage = np.array(image, dtype=np.float32) #image = np.expand_dims(image, 0) fimage = np.divide(fimage, 255.) tm_inf = time.time() graph.queue_inference_with_fifo_elem(fifoIn, fifoOut, fimage, 'user object') prediction, _ = fifoOut.read_elem() #prediction = np.add(fimage, 128) grid = 8 prediction = np.reshape(prediction, (grid, grid, 5, 4 + 1 + nb_class)) # prediction = np.multiply(prediction, 255) fps_img = ( fps_img + ( 1 / (time.time() - start) ) ) / 2 print( "Inference time: {:.4f}".format(time.time() - tm_inf) ) # predictions decoding boxes = decode_netout(prediction, anchors, nb_class) image = draw_boxes(image, boxes, config['model']['labels']) image = cv2.putText(image, "fps: {:.2f}".format(fps_img), (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 4 ) cv2.imshow("Press q to quit", image) fps.update() #if counter == 10: #print(image.sum(), boxes) # time.sleep(1) counter += 1 if cv2.getWindowProperty( "Press q to quit", cv2.WND_PROP_ASPECT_RATIO ) < 0.0: print("Window closed" ) break elif cv2.waitKey( 1 ) & 0xFF == ord( 'q' ): print( "Q pressed" ) break fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cap.release() else: #detected_images_path = os.path.join(image_path, "detected") #if not os.path.exists(detected_images_path): # os.mkdir(detected_images_path) images = list(list_images(source)) for fname in images[100:]: image = cv2.imread(fname) tm_inf = time.time() boxes = yolo.predict(image) print( "Inference time: {:.4f}".format(time.time() - tm_inf) ) image = draw_boxes(image, boxes, config['model']['labels']) cv2.imshow("Press q to quit", image) if cv2.waitKey( 1 ) & 0xFF == ord( 'q' ): break time.sleep(2) # fname = os.path.basename(fname) # cv2.imwrite(os.path.join(image_path, "detected", fname), image) cv2.destroyAllWindows()
def main_process(self): input = self.filename # cap = cv2.VideoCapture(input) #reader = imageio.get_reader(input) #fps = reader.get_meta_data()['fps'] #writer = imageio.get_writer('E:\PROJECT\Traffic-Signal-Violation-Detection-System-master\Traffic-Signal-Violation-Detection-System-master\Resources\output\output.mp4', fps = fps) base = "yolo-coco" labelsPath = os.path.sep.join([base, "coco.names"]) LABELS = open(labelsPath).read().strip().split("\n") weightsPath = os.path.sep.join([base, "yolov3.weights"]) configPath = os.path.sep.join([base, "yolov3.cfg"]) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromDarknet(configPath, weightsPath) net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) ln = net.getLayerNames() ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] #retrieve yolo layers print("[INFO] opening video file...") vs = cv2.VideoCapture(input) # 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=10, maxDistance=50) trackers = [] trackableObjects = {} TRAFFIC_LIGHT_CONFIDENT_VALUE = 5000 # 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 totalUp = 0 sys_init = False COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8") # start the frames per second throughput estimator fps = FPS().start() light_color = "" color = "" # 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 we are viewing a video and we did not grab a frame then we # have reached the end of the video if 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 if sys_init == False: #traff_sel_roi = cv2.selectROI("Select Traffic Light", frame, False, False) #cv2.destroyWindow("Select Traffic Light") #traffic_light = [(traff_sel_roi[0], traff_sel_roi[1]), (traff_sel_roi[0] + traff_sel_roi[2], traff_sel_roi[1] + traff_sel_roi[3])] if not path.exists(input + 'ROI1.txt'): sel_roi = cv2.selectROI("Select Monitor Line", frame, False, False) cv2.destroyWindow("Select Monitor Line") mon_line = [(sel_roi[0], sel_roi[1]), (sel_roi[0] + sel_roi[2], sel_roi[1] + sel_roi[3])] fp = open(input + "ROI1.txt", 'w') for i in sel_roi: fp.write('{}\n'.format(str(i))) fp.close() if not path.exists(input + 'traffic.txt'): tra_roi = cv2.selectROI("Select Traffic", frame, False, False) cv2.destroyWindow("Select Traffic light") mon_line = [(sel_roi[0], sel_roi[1]), (sel_roi[0] + sel_roi[2], sel_roi[1] + sel_roi[3])] fp = open(input + "traffic.txt", 'w') for i in tra_roi: fp.write('{}\n'.format(str(i))) fp.close() sys_init = True fp = open(input + 'ROI1.txt', 'r') ROI = [] for line in fp: line = line.strip() ROI.append(int(line)) mon_line = [(ROI[0], ROI[1]), (ROI[0] + ROI[2], ROI[1] + ROI[3])] fp = open(input + 'traffic.txt', 'r') TOI = [] for line in fp: line = line.strip() TOI.append(int(line)) traffic_ligh = frame[int(TOI[1]):int(TOI[1] + TOI[3]), int(TOI[0]):int(TOI[0] + TOI[2])] rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) traffic_light = 0 # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] status = "Waiting" rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % 10 == 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, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) layeroutputs = net.forward(ln) confidences = [] boxes = [] classID = [] classes = ["car", "motorbike", "truck"] # loop over the detections for layer in layeroutputs: for i, detection in enumerate(layer): class_scores = detection[5:] confidence = detection[4] class_id = np.argmax(class_scores) class_score = class_scores[class_id] if LABELS[class_id] not in classes: continue if (confidence) > 0.5: confidences.append(float(confidence)) BOX = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, Width, Height) = BOX.astype("int") startX = int(centerX - (Width / 2)) startY = int(centerY - (Height / 2)) boxes.append( [startX, startY, int(Width), int(Height)]) classID.append(class_id) idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.5) if len(idxs) > 0: # loop over the indexes we are keeping for i in idxs.flatten(): # extract the bounding box coordinates (x, y) = (boxes[i][0], boxes[i][1]) (w, h) = (boxes[i][2], boxes[i][3]) if (classID[i] == 9): xlight, ylight, wlight, hlight = (x, y, w, h) endX = x + w endY = y + h # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker tracker = dlib.correlation_tracker() rect = dlib.rectangle(x, y, 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, LABELS[classID[i]])) # 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, id 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()) rects.append((startX, startY, endX, endY, id)) # draw a horizontal line in the center of the frame -- once an # object crosses this line we will determine whether they jumped the red light or not cv2.line(frame, mon_line[0], mon_line[1], (0, 0, 255), thickness=1) # use the centroid tracker to associate the (1) old object # centroids with (2) the newly computed object centroids #light = frame[ylight:ylight + hlight, xlight:xlight + wlight] hsv = cv2.cvtColor( traffic_ligh, cv2.COLOR_BGR2HSV ) #light = frame[ylight:ylight + hlight, xlight:xlight + wlight] sum_saturation = np.sum( hsv[:, :, 1] ) # Sum the brightness values#light = frame[ylight:ylight + hlight, xlight:xlight + wlight] area = 32 * 32 #light = frame[ylight:ylight + hlight, xlight:xlight + wlight] avg_saturation = sum_saturation / area #light = frame[ylight:ylight + hlight, xlight:xlight + wlight] sat_low = int( avg_saturation * 1.3 ) #light = frame[ylight:ylight + hlight, xlight:xlight + wlight] val_low = 140 #light = frame[ylight:ylight + hlight, xlight:xlight + wlight] lower_red1 = np.array([150, sat_low, val_low]) #b, g, r=cv2.split(traffic_ligh) upper_red1 = np.array([180, 255, 255]) #b, g, r=cv2.split(traffic_ligh) lower_green = np.array([70, sat_low, val_low]) #b, g, r=cv2.split(traffic_ligh) upper_green = np.array([100, 255, 255]) #b, g, r=cv2.split(traffic_ligh) lower_yellow = np.array([10, sat_low, val_low ]) #b, g, r=cv2.split(traffic_ligh) upper_yellow = np.array([60, 255, 255]) #b, g, r=cv2.split(traffic_ligh) maskr = cv2.inRange(hsv, lower_red1, upper_red1) #b, g, r=cv2.split(traffic_ligh) #b, g, r=cv2.split(traffic_ligh) #b, g, r = cv2.split(traffic_ligh) maskg = cv2.inRange(hsv, lower_green, upper_green) #traffic_ligh=cv2.merge([r,g,b]) masky = cv2.inRange(hsv, lower_yellow, upper_yellow) #traffic_ligh=cv2.merge([r,g,b]) #traffic_ligh=cv2.merge([r,g,b]) r_circles = cv2.HoughCircles( maskr, cv2.HOUGH_GRADIENT, 1, 80, param1=50, param2=10, minRadius=0, maxRadius=30) #maskr = cv2.add(mask1, mask2) y_circles = cv2.HoughCircles( masky, cv2.HOUGH_GRADIENT, 1, 60, param1=50, param2=10, minRadius=0, maxRadius=30) #maskr = cv2.add(mask1, mask2) g_circles = cv2.HoughCircles( maskg, cv2.HOUGH_GRADIENT, 1, 30, param1=50, param2=5, minRadius=0, maxRadius=30) #maskr = cv2.add(mask1, mask2) if r_circles is not None: light_color = "Red" color = "red" if y_circles is not None: color = "orange" if g_circles is not None: color = "green" #b, g, r = cv2.split(traffic_ligh) #traffic_ligh=cv2.merge([r,g,b]) #if(trafficLightColor.estimate_label(traffic_ligh)=="Red"): # color="Red" # light_color="Red" #if(trafficLightColor.estimate_label(traffic_ligh)=="Yellow"): # color="Orange" #if(trafficLightColor.estimate_label(traffic_ligh)=="Green"): # color="Green" cv2.putText(frame, color, (TOI[0], TOI[1]), cv2.FONT_HERSHEY_DUPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) objects = ct.update(rects) labels = ct.labels boundingboxes = ct.boundingbox # loop over the tracked objects #traffic_light_crop = frame[int(traff_sel_roi[1]):int(traff_sel_roi[1]+traff_sel_roi[3]), int(traff_sel_roi[0]):int(traff_sel_roi[0]+traff_sel_roi[2])] for (objectID, centroid) in objects.items(): box = boundingboxes.get(objectID) #traffic_color=trafficshape.detect(frame) box = boundingboxes.get(objectID) text = "ID {}".format(objectID) #draw bounding box for each object #traffic_light_crop = frame[int(traff_sel_roi[1]):int(traff_sel_roi[1]+traff_sel_roi[3]), int(traff_sel_roi[0]):int(traff_sel_roi[0]+traff_sel_roi[2])] #hsv_traffic_light_crop = cv2.cvtColor(traffic_light_crop, cv2.COLOR_BGR2HSV) #hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)#low_red = np.array([161, 155, 84], np.uint8) #high_red = np.array([179, 255, 255], np.uint8) #traffic_signal_mask = cv2.inRange(hsv_traffic_light_crop, low_red, high_red) #if np.sum(traffic_signal_mask) > TRAFFIC_LIGHT_CONFIDENT_VALUE: # color='red' #if color=="red": # light_color="Red" #if color == "yellow": # light_color = "Yellow" #if color == "green": # light_color = "Green" cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), color=(0, 255, 0), thickness=1) # 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) #when the light turns red, save the first position for each vehicle if light_color == "Red" and to.firstpos == 0: to.firstpos = centroid[1] to.centroids.append(centroid) if to.counted == True: #when the vehicle passed the line, we mark it with red color bounding box. cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), color=(0, 0, 255), thickness=2) # check to see if the object has been counted or not and the first position is below the line if not to.counted and to.firstpos > (mon_line[0][1] + mon_line[1][1]) / 2: if direction < -3 and centroid[1] < ( mon_line[0][1] + mon_line[1][1]) / 2 and light_color == "Red": totalUp += 1 cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), color=(0, 0, 255), thickness=1) traffic_light = frame[box[1]:box[3], box[0]:box[2]] save_dir = "./img/{:DATE_%d-%m-%Y_TIME_%H-%M-%S-%f}_Normal.png".format( datetime.now()) cv2.imwrite(save_dir, traffic_light) to.counted = True # store the trackable object in our dictionary trackableObjects[objectID] = to # construct a tuple of information we will be displaying on the # frame info = [ ("Violation", totalUp), ] # 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_DUPLEX, 1, (255, 255, 255), 2) # show the output frame #self.show_image(frame) cv2.imshow('Traffic', frame) key = cv2.waitKey(1) & 0xFF 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())) root.deiconify() # close any open windows cv2.destroyAllWindows()
def main(): """ :return: """ # Display best params or run the whole sim if DISPLAY_BEST_PARAMS: try: with open(PATH_BEST_PARAMS, 'rb') as f: best_params, best_recall, best_precision = pickle.load(f) except FileNotFoundError: print("[ERROR] Best param file not found.") raise iteration_dict = [best_params, best_recall, best_precision] # Matches the dump order else: params = { 'gaussWindow': range(1, 8, 2), 'residualConnections': range(1, 8, 2), 'sigma': np.linspace(0.1, 0.9, 5), 'dilationIterations': range(1, 8, 2), 'mgp': range(25, 26, 25), 'winSize': range(3, 4, 2), 'maxLevel': range(5, 6, 3), 'threshold_low': range(65, 66, 10), 'threshold_gain': np.linspace(1.25, 1.26, 1), 'diffMethod': range(0, 1, 1), 'skipFrame': range(0, 1, 1) } header = create_log(PATH_ALL_RESULTS, params) iteration_dict = ParameterGrid(params) video_stream, nb_frames, frame_width, frame_height = vt.init.import_stream( VIDEO_STREAM_PATH) bbox_heli_ground_truth = vt.bbox.import_bbox_heli( PATH_BBOX) # Creates a dict # Min/Max area for the helicopter detection. # Min is difficult: it could be as small as a speck in the distance # Max is easier: you know how close it can possibly get (the helipad) min_area = 1 if ((frame_width == 1920 and frame_height == 1080) or (frame_width == 3280 and frame_height == 2464)): binning = 1 else: binning = 2 * 2 print( "[WARNING] Input resolution unusual. Camera sensor understood to be working with a 2x2 binning." ) max_area = 200 * 200 / binning print("[INFO] Starting {} iterations".format(len(iteration_dict))) first_bbox = min(bbox_heli_ground_truth.keys()) last_bbox = max(bbox_heli_ground_truth.keys()) print("[INFO] Using bbox frames {} to {}".format(first_bbox, last_bbox)) # Save the best results in memory if DISPLAY_BEST_PARAMS: counter_best_params = 0 # Used when displaying the 3 best runs highest_f1_score = 0 highest_recall = 0 highest_precision = 0 vs2 = vt.init.cache_video(video_stream, 'list', gray_scale=FLAG_GRAY_SCALE) for sd in tqdm.tqdm(iteration_dict): # ------------------------------------- # 1. RESET THE SIM DEPENDENT VARIABLES # ------------------------------------- timing = { 'Read frame': 0, 'Convert to grayscale': 0, 'Stabilize': 0, 'Double Gauss': 0, 'Abs diff': 0, 'Thresholding': 0, 'Dilation': 0, 'Count boxes': 0, 'Finalize': 0 } nb_bbox = [] # Stores bbox data for a sim # Get ready to store residualConnections frames over and over previous_gray_frame = collections.deque( maxlen=sd['residualConnections']) # previous_gauss_frame = collections.deque(maxlen=sd['residualConnections']) # img_stab = imageStabilizer.imageStabilizer(frame_width, frame_height, maxGoodPoints=sd['mgp'], # maxLevel=sd['maxLevel'], winSize=sd['winSize']) counter_skip_frame = sd[ 'skipFrame'] # Go through the if statement the first time fps = FPS().start() # ---------------------------- # 2. FRAME PROCESSING - GO THROUGH ALL FRAMES WITH A BBOX # ----------------------------- for frame_number in range(nb_frames): t0 = time.perf_counter() # frame = vs.read()[1] # No cache current_frame = vs2[frame_number].copy( ) # Prevents editing the original frames! t1 = time.perf_counter() # Skip all the frames that do not have a Bbox if frame_number < first_bbox: continue if frame_number > min(nb_frames - 2, last_bbox): break # 0. Skip frames - subsampling of FPS if counter_skip_frame < sd['skipFrame']: counter_skip_frame += 1 continue else: counter_skip_frame = 0 # Create a 0 based index that tracks how many bboxes we have gone through bbox_frame_number = frame_number - first_bbox # Starts at 0, automatically incremented # Populate the deque with sd['residualConnections'] gray frames if bbox_frame_number < sd['residualConnections']: # current_frame = frame current_gray_frame = current_frame if FLAG_GRAY_SCALE else cv2.cvtColor( current_frame, cv2.COLOR_BGR2GRAY) previous_gray_frame.append(current_gray_frame) continue # I. Grab the current in color space # t0=time.perf_counter() # current_frame = frame # II. Convert to gray scale t2 = time.perf_counter() current_gray_frame = current_frame if FLAG_GRAY_SCALE else cv2.cvtColor( current_frame, cv2.COLOR_BGR2GRAY) # III. Stabilize the image in the gray space with latest gray frame, fwd to color space # Two methods (don't chain them): phase correlation & optical flow t3 = time.perf_counter() if FLAG_PHASE_CORRELATION: """[TBR/Learning XP] Phase correlation is linearly faster as the area to process is reduced, which is nice. However, if the expected translation is small (like ~ 1px) the results predictions can vary widely as the crop size is reduced. If the motion gets larger (even just 10 px), results between small and large crop match very accurately! plt.figure() plt.imshow(crop) plt.show() lCrop = 1000 # Large crop motion = 10 # controlled displacement for sCrop in range(100, 1001, 100): #sCrop = 200 t31 = time.perf_counter() retvalSmall, response = cv2.phaseCorrelate(np.float32(current_gray_frame[:sCrop, :sCrop])/255.0, np.float32(current_gray_frame[motion:sCrop+motion, motion:sCrop+motion])/255.0) t32 = time.perf_counter() retvalLarge, response = cv2.phaseCorrelate(np.float32(current_gray_frame[:lCrop, :lCrop])/255.0, np.float32(current_gray_frame[motion:lCrop+motion, motion:lCrop+motion])/255.0) t33 = time.perf_counter() print("Full image is {} bigger and takes {} more time" .format((lCrop/sCrop)**2, (t33-t32)/(t32-t31))) print("xs {:.3f} xl {:.3f} Rx={:.3f} ys {:.3f} yl {:.3f} Ry={:.3f}".format( retvalSmall[0], retvalLarge[0], retvalSmall[0]/retvalLarge[0], retvalSmall[1], retvalLarge[1], retvalSmall[1]/retvalLarge[1])) assert 1==0 """ pass if FLAG_OPTICAL_FLOW: m, current_gray_frame = img_stab.stabilizeFrame( previous_gray_frame[-1], current_gray_frame) current_frame = cv2.warpAffine(current_frame, m, (frame_width, frame_height)) t4 = time.perf_counter() # current_frame = current_frame[int(cropPerc*frame_height):int((1-cropPerc)*frame_height), # int(cropPerc*frame_width):int((1-cropPerc)*frame_width)] # modif[bbox_frame_number-1] = img_stab.extractMatrix(m) # IV. Gaussian Blur # Done between current_frame and the grayFrame from residualConnections ago (first element in the deque) current_gauss_frame = cv2.GaussianBlur( current_gray_frame, (sd['gaussWindow'], sd['gaussWindow']), 0) previous_gauss_frame = cv2.GaussianBlur( previous_gray_frame[0], (sd['gaussWindow'], sd['gaussWindow']), 0) t5 = time.perf_counter() # V. Differentiation in the Gaussian space diff_frame = cv2.absdiff(current_gauss_frame, previous_gauss_frame) """[TBR/XP] absdiff strategies in the gaussian space""" """#Average of the absdiff with the current_frame for all residual connections (1toN strategy) # Basically, you do (1/m)*sum(|current_frame-previousGauss[i]|, i=0..N), # N being dictated by residualConnections diff_frame = np.zeros(current_gauss_frame.shape) for gaussFrame in previous_gauss_frame: diff_frame += cv2.absdiff(current_gauss_frame, gaussFrame) diff_frame /= len(previous_gauss_frame) diff_frame = diff_frame.astype(np.uint8) # float -> uint8 # Best f1_score was about 0.32 (0.34 for simple absdiff(N, N-k)) """ """#Average of the absdiff between n and n-1 frame (NtoN-1 strategy) # Basically, you do (1/m)*sum(|previousGauss[i]-previousGauss[i+1]|, i=0..N-1), # N being dictated by residualConnections # In that case, an array of the differences in the gaussian space could be cached to just pick # what you want, but there is not enough RAM. diff_frame = np.zeros(current_gauss_frame.shape) for index in range(len(previous_gauss_frame)-1): diff_frame += cv2.absdiff(previous_gauss_frame[index], previous_gauss_frame[index+1]) # Finish with current_gauss_frame and the latest previous_gauss_frame diff_frame += cv2.absdiff(current_gauss_frame, previous_gauss_frame[-1]) diff_frame /= len(previous_gauss_frame) diff_frame = diff_frame.astype(np.uint8) # float -> uint8 # Best f1_score was about 0.29 (0.34 for simple absdiff(N, N-k)) """ t6 = time.perf_counter() if DISPLAY_FEED != '000': delta_frame = diff_frame.copy() # VI. BW space manipulations # diff_frame = cv2.threshold(diff_frame, sd['threshold'], 255, cv2.THRESH_BINARY)[1] # v = np.median(diff_frame) v = 127 lower = int(max(0, (1.0 - sd['sigma']) * v)) upper = int(min(255, (1.0 + sd['sigma']) * v)) # diff_frame = cv2.Canny(diff_frame, sd['threshold_low'], sd['threshold_low']*sd['threshold_gain']) diff_frame = cv2.Canny(diff_frame, lower, upper) t7 = time.perf_counter() # dilate the thresholded image to fill in holes, then find contours if sd['diffMethod'] == 0: diff_frame = cv2.dilate(diff_frame, None, iterations=sd['dilationIterations']) diff_frame = cv2.erode(diff_frame, None, iterations=sd['dilationIterations']) elif sd['diffMethod'] == 1: diff_frame = cv2.morphologyEx(diff_frame, cv2.MORPH_OPEN, None) if DISPLAY_FEED != '000': thresh_feed = diff_frame.copy() cnts = cv2.findContours(diff_frame, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) t8 = time.perf_counter() # Circle around the actual corner of the helicoBBox # Obtained via manual CSRT TRACKER # cv2.circle(current_frame, bbox_heli_ground_truth[bbox_frame_number], BBOX_ERROR, (0,0,255), -1) large_box = 0 counter_bbox_heli = 0 # VII. Process the BB and classify them x_gt, y_gt, w_gt, h_gt = bbox_heli_ground_truth[ frame_number] # Ground Truth data for c in cnts: # A. Filter out useless BBs # 1. if the contour is too small or too large, ignore it if cv2.contourArea(c) < min_area: continue if cv2.contourArea(c) > max_area: continue # compute the bounding box for the contour, draw it on the current_frame, # and update the text (x, y, w, h) = cv2.boundingRect(c) # 2. Box partially out of the frame # if x < 0 or x+s > frame_width or y < 0 or y+s > frame_height: # Square box if x < 0 or x + w > frame_width or y < 0 or y + h > frame_height: continue # 3. Box center in the PADDING area if not (PADDING < x + w // 2 < frame_width - PADDING and PADDING < y + h // 2 < frame_height - PADDING): continue # B. Classify BBs - a large_box is a potential bbox_heli_ground_truth large_box += 1 # Check if the corner is within range of the actual corner # That data was obtained by running a CSRT TRACKER on the helico # Classify bboxes based on their IOU with ground truth converted_current_bbox = vt.bbox.xywh_to_x1y1x2y2((x, y, w, h)) converted_ground_truth_bbox = vt.bbox.xywh_to_x1y1x2y2( (x_gt, y_gt, w_gt, h_gt)) if vt.bbox.intersection_over_union( converted_current_bbox, converted_ground_truth_bbox) >= IOU: counter_bbox_heli += 1 if DISPLAY_FEED == '001': # Display positive bbox found in COLOR['GREEN'] cv2.putText(current_frame, "heli", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLOR['GREEN'], 2) cv2.rectangle(current_frame, (x, y), (x + w, y + h), COLOR['GREEN'], 2) else: if DISPLAY_FEED == '001': # Display negative bbox found in COLOR['BLUE'] cv2.putText(current_frame, "not heli", (x, y + h + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLOR['BLUE'], 2) cv2.rectangle(current_frame, (x, y), (x + w, y + h), COLOR['BLUE'], 2) pass # C. Generate a square BB # cv2.rectangle(current_frame, (x, y), (x + s, y + s), COLOR['GREEN'], 2) # cv2.rectangle(current_frame, (x, y), (x + w, y + h), COLOR['GREEN'], 2) if DISPLAY_FEED == '001': cv2.rectangle(current_frame, (x_gt, y_gt), (x_gt + w_gt, y_gt + h_gt), COLOR['RED'], 2) t9 = time.perf_counter() # VIII. draw the text and timestamp on the current_frame if DISPLAY_FEED != '000': if DISPLAY_BEST_PARAMS: if counter_best_params == 0: run = "best_f1_score" elif counter_best_params == 1: run = "best_recall" elif counter_best_params == 2: run = "best_precision" else: raise ValueError( 'There should only be 3 best results in the best_param log file' ) cv2.putText( current_frame, "Current run: {} - f1_score: {:.3f} - recall: {:.3f} - precision: {:.3f}" .format(run, sd['f1_score'], sd['recall'], sd["precision"]), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLOR['RED'], 2) cv2.putText( current_frame, "BBoxes: {} found, {} heliBox".format( len(cnts), counter_bbox_heli), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLOR['RED'], 2) # cv2.putText(current_frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), (10, 30), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLOR['RED'], 1) # Shows current date/time # IX. show the current_frame and record if the user presses a key show_feed(DISPLAY_FEED, thresh_feed, delta_frame, current_frame) key = cv2.waitKey(1) & 0xFF # if the `q` key is pressed, break from the loop if key == ord("q"): break # X. Save frames & track KPI # The deque has a maxlen of residualConnections so the first-in will pop gray_frame_to_append = vs2[ frame_number] if FLAG_GRAY_SCALE else cv2.cvtColor( vs2[frame_number], cv2.COLOR_BGR2GRAY) previous_gray_frame.append(gray_frame_to_append) nb_bbox.append([ len(cnts), large_box, counter_bbox_heli, 1 if counter_bbox_heli else 0 ]) fps.update() t10 = time.perf_counter() if FLAG_DISPLAY_TIMING: new_timing = { 'Read frame': t1 - t0, 'Convert to grayscale': t3 - t2, 'Stabilize': t4 - t3, 'Double Gauss': t5 - t4, 'Abs diff': t6 - t5, 'Thresholding': t7 - t6, 'Dilation': t8 - t7, 'Count boxes': t9 - t8, 'Finalize': t10 - t9 } for key in timing.keys(): timing[key] += new_timing[key] # XI. Display results fps.stop() # vs.release() # Done with going through this simulation, get ready for another pass if FLAG_DISPLAY_TIMING: print("Code profiling for various operations (in s):\n", timing) cv2.destroyAllWindows() average_fps = fps.fps() print("[INFO] FPS: {:.2f}".format(average_fps)) # print(img_stab.detailedTiming()) # Impact of stabilization on number of boxes bb = np.array(nb_bbox) bb = bb[1:] # Delete first frame which is not motion controlled # KPI # per simulation # print(bb) avg_nb_boxes = np.mean(bb[:, 0]) avg_nb_filtered_boxes = np.mean(bb[:, 1]) avg_nb_heli_bbox = np.mean(bb[:, 2]) # Precision: how efficient is the algo at rulling out irrelevant boxes? precision = avg_nb_heli_bbox / avg_nb_filtered_boxes # Ratio of helibox/nb of boxes # Recall: how many frames had a positive heliBox? There should be one in each. recall = np.sum( bb[:, 3]) / nb_frames # Proportion of frames with helicopter # ----------------- # SANITY CHECKS & f1_score # ----------------- try: assert 0 < recall <= 1 assert 0 < precision <= 1 assert 0 <= avg_nb_heli_bbox <= avg_nb_filtered_boxes assert 0 <= avg_nb_filtered_boxes <= avg_nb_boxes f1_score = 2 / (1 / precision + 1 / recall) except AssertionError: print('[WARNING] KPIs out of bounds - set to 0') print("[WARNING] KPI: ", recall, precision, avg_nb_heli_bbox, avg_nb_filtered_boxes) recall, precision, avg_nb_heli_bbox, avg_nb_filtered_boxes = (0, 0, 0, 0) f1_score = 0 """kpis plt.figure() plt.plot(bb[:, 0]) plt.plot(bb[:, 1]) plt.plot(bb[:, 2]) plt.legend(("Number of boxes", "Boxes large enough", "Heli box")) titl = \ "Boxes detected - av: {:.2f} - std: {:.2f} at {:.2f} FPS\n\ Av Helibb per frame: {:.3f} - Ratio of helibb: {:.3f}\tFrame with heli: {:.3f} "\ .format(\ avg_nb_filtered_boxes, np.std(bb[:, 1]), real_fps, \ avg_nb_heli_bbox, precision, recall\ ) plt.title(titl) plt.show() """ # Display best params or append best results to log if DISPLAY_BEST_PARAMS: counter_best_params += 1 # print(sd) # Possible to limit digits? print( 'gaussWindow: {}, residualConnections: {}, sigma: {:.1f}, dilationIterations: {}, precision: {:.3f}, recall: {:.3f}, f1_Score: {:.3f}' .format(sd['gaussWindow'], sd['residualConnections'], sd['sigma'], sd['dilationIterations'], sd['precision'], sd['recall'], sd['f1_score'])) # Possible to limit digits? else: # Output results - parameters+kpis kpis = [ IOU, average_fps, avg_nb_boxes, avg_nb_filtered_boxes, avg_nb_heli_bbox, precision, recall, f1_score ] # Warning: they are both int array of the same length so they can be added! sim_output = [sd[k] for k in params.keys()] + list(kpis) # Log the best f1_score, recall and precision if f1_score > highest_f1_score: highest_f1_score = f1_score best_params = sim_output if recall > highest_recall: highest_recall = recall best_recall = sim_output if precision > highest_precision: highest_precision = precision best_precision = sim_output with open(PATH_ALL_RESULTS, 'a') as f: w = csv.writer(f) w.writerow(sim_output) # XII. Wrap-up the search & output some logs for quick review # XII.1. Save the best param after inputting the header if not DISPLAY_BEST_PARAMS: create_log(PATH_BEST_PARAMS, params) with open(PATH_BEST_PARAMS, 'a') as f: out = csv.writer(f) #out.writeheader() out.writerow(best_params) out.writerow(best_precision) out.writerow(best_recall) with open(PATH_PARAM_SPACE, 'w') as f: out = csv.DictWriter(f, fieldnames=header) out.writerow(params) """[TBR] No more dict pickling, use DictWriter instead so they are human readable with open(PATH_BEST_PARAMS, 'wb') as f: best_params = dict(zip(header, best_params)) best_precision = dict(zip(header, best_precision)) best_recall = dict(zip(header, best_recall)) pickle.dump([best_params, best_recall, best_precision], f, protocol=pickle.HIGHEST_PROTOCOL) # XII.2. Pickle the params dict with open(PATH_PARAM_SPACE, 'wb') as f: pickle.dump(params, f, protocol=pickle.HIGHEST_PROTOCOL) """ # XII.3. Final message!! print("Done. Highest f1_score: ", highest_f1_score)
def Play(self): if not self.is_Train: print('load image_model ...') self.net = cv2.dnn.readNetFromCaffe(self.prorotxt_path, self.caffe_model_path) with tf.Session() as sess: sess.run(self.init) if self.load_model == True: print('load_model ...') ckpt = tf.train.get_checkpoint_state(path) saver.restore(sess, ckpt.model_checkpoint_path) for i in range(self.num_episodes): if not self.is_Train: CLASSES = ['bottle'] #["background", "aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", # "chair", "cow", "diningtable","dog", "horse", "motorbike", "person", "pottedplant", # "sheep","sofa", "train", "tvmonitor" self.col = -1 self.width = -1 self.row = -1 self.height = -1 self.frame = None self.frame2 = None self.inputmode = False self.rectangle = False self.trackWindow = None self.roi_hist = None self.cap = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() cv2.namedWindow('frame') cv2.setMouseCallback('frame', self.onMouse, param=(self.frame, self.frame2)) termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) episode_buffer = experience_buffer() state = self.game.Reset() state = self.processState(state) dead = False reward_all = 0 while True: if not self.is_Train: is_game_start = False self.frame = self.cap.read() #print(self.frame) self.frame = imutils.resize(self.frame, width=200, height=200) (h, w) = self.frame.shape[:2] blob = cv2.dnn.blobFromImage( cv2.resize(self.frame, (300, 300)), 0.007843, (300, 300), 127.5) self.net.setInput(blob) detections = self.net.forward() self.obstacle_points = [] for x in np.arange(0, detections.shape[2]): confidence = detections[0, 0, x, 2] if confidence > 0.2: ### set for changing idx = int(detections[0, 0, x, 1]) box = detections[0, 0, x, 3:7] * np.array( [w, h, w, h]) (startX, startY, endX, endY) = box.astype('int') label = "{}: {:.2f}%".format( 'obstacle', confidence * 100) cv2.rectangle(self.frame, (startX, startY), (endX, endY), self.obstacle_box_color, 2) self.obstacle_points.append({ 'row': startY, 'col': startX, 'row_size': endY - startY, 'col_size': endX - startX }) if self.trackWindow is not None: hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], self.roi_hist, [0, 180], 1) ## Maybe this window is track size ret, self.trackWindow = cv2.meanShift( dst, self.trackWindow, termination) x, y, w, h = self.trackWindow self.target_point = { 'row': int((2 * y + h) / 2), 'col': int((2 * x + w) / 2) } cv2.rectangle(self.frame, (x, y), (x + w, y + w), (0, 255, 0), 3) is_game_start = True else: self.target_point = { 'row': -1, 'col': -1 } #in Sim m_row == -1 is_show = False show_frame = cv2.resize(self.frame, None, fx=2, fy=2) cv2.imshow('frame', show_frame) print(self.target_point) key = cv2.waitKey(60) & 0xFF if key == ord('i'): print('select target') self.inputmode = True self.frame2 = self.frame.copy() while self.inputmode: cv2.imshow('frame', self.frame) cv2.waitKey(0) fps.update() ### Idont know where it locatied if not is_game_start: continue else: self.game.Update_ob_points(self.target_point, self.obstacle_points) action = sess.run( self.mainQN.predict, feed_dict={self.mainQN.scalarInput: [state]})[0] state_1, reward, dead = self.game.Step(action) if dead == True: break self.jList.append(epi) self.rList.append(reward_all) f = open('./play_graph.txt', 'a') f.write(str(i) + '_th Game_End = Reward : ' + str(reward_all)) f.write('\n') f.close() self.game.Print_action_log() print(str(i) + '_th Game_End = Reward : ' + str(reward_all)) if not self.is_Train: cv2.destroyAllWindows() print("Percent of succesful episodes: " + str(sum(self.rList) / self.num_episodes) + "%") rMat = np.resize(np.array(self.rList), [len(self.rList) // 100, 100]) rMean = np.average(rMat, 1) plt.plot(rMean)
def main(): print('[INFO] loading model...') net = cv2.dnn.readNetFromCaffe(args['prototxt'], args['model']) print('[INFO] starting video stream...') cap = cv2.VideoCapture(args['video']) time.sleep(1.0) fps = FPS().start() # loop over the frames from the video stream while cap.isOpened(): _, frame = cap.read() if args['reshape'] is True: frame = imutils.resize(frame, width=args['width'], height=args['height']) # grab the frame dimensions, convert it to a blob and feed the network (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() # initiate an empty list for containing predicted objects obj_list = [] # loop over detections for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] # filter out weak predictions. 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), BBOX_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, BBOX_COLORS[idx], 2) obj_list.append(CLASSES[idx]) # Display objects count for j, (pred_class, n_class) in enumerate( x for x in set(map(lambda x: (x, obj_list.count(x)), obj_list))): cv2.putText(img=frame, text='{}: {}'.format(pred_class, n_class), org=(5, 40 * (j + 1)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, color=COUNTER_COLOR, thickness=2) # show the output frame cv2.imshow('Frame', frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord('q'): break # 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() cap.release()
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 get_frame(self): sift = cv2.xfeatures2d.SIFT_create() fps = FPS().start() if self.localize_method == 3: self.Detector = Localize2.DetectorMorph() if self.localize_method == 2: self.Detector = Localize1.DetectorEdges() if self.localize_method == 1: self.Detector = Localize3.DetectorMorph() if self.exctact_method == 1: self.Extractor = Segmentation_chars.Segmentation() if self.exctact_method == 2: self.Extractor = Segmentation_chars2.Segmentation2() j = 0 while self.video.get(cv2.CAP_PROP_POS_FRAMES) < self.num_frames and self.stopped == False: #flag when plate is found on frame found = 0 ret, self.last_frame = self.video.read() frame = cv2.resize(self.last_frame, None, fx=0.5, fy=0.5) tablice = [] numbers = [] if self.roi == 1: frame_roi = frame[int(self.y*(frame.shape[0]/400)):int(self.y*(frame.shape[0]/400)+self.height*(frame.shape[0]/400)), int(self.x*(frame.shape[1]/630)):int(self.x*(frame.shape[1]/630)+self.width*(frame.shape[1]/630))] candidates,xy_candidates = self.Detector.find_plate(frame_roi) if self.roi == 2: candidates, xy_candidates = self.Detector.find_plate(frame) for i in range(0,len(candidates)): cand = self.last_frame[(xy_candidates[i][0]+int(self.y*(frame.shape[0]/400))) * 2: (xy_candidates[i][2]+int(self.y*(frame.shape[0]/400))) * 2, (xy_candidates[i][1]+int(self.x*(frame.shape[1]/630))) * 2: (xy_candidates[i][3]+int(self.x*(frame.shape[1]/630))) * 2] if self.flag==1: j = j + 1 filename = "tab/222_" + "%s.png" % str(j) cv2.imwrite(filename,cand) result = self.find_plate(cand) number = "" if result == 0: tablice.append(xy_candidates[i]) chars = self.Extractor.segment(cand) plate_numbers = [] for char in chars: plate_numbers.append(self.predict_character(char)) plate_numbers, ok = self.syntactic.check(plate_numbers) for char in plate_numbers: if ok == 2: number = number + self.class_names[char] numbers.append(number) cv2.rectangle(frame, (xy_candidates[i][1]+int(self.x*(frame.shape[1]/630)), xy_candidates[i][0]+int(self.y*(frame.shape[0]/400))), (xy_candidates[i][3]+int(self.x*(frame.shape[1]/630)), xy_candidates[i][2]+int(self.y*(frame.shape[0]/400))), (0, 255, 0), 2) obiekt, text, rect,missing = self.tracker.update(tablice,numbers) szukaj = [] miss = [] for im in missing: if isinstance(im[0], list): mis = self.last_frame2[(im[0][0] + int(self.y * (frame.shape[0] / 400))) * 2: (im[0][2] + int(self.y * (frame.shape[0] / 400))) * 2,(im[0][1] + int(self.x * (frame.shape[1] / 630))) * 2: (im[0][3] + int(self.x * (frame.shape[1] / 630))) * 2] x0 = (im[0][1] + int(self.x * (frame.shape[1] / 630))) *2 - 20 x1 = (im[0][3] + int(self.x * (frame.shape[1] / 630))) * 2 + 20 y0 = (im[0][0] + int(self.y * (frame.shape[0] / 400))) * 2 - 10 y1 = (im[0][2] + int(self.y * (frame.shape[0] / 400))) * 2 + 10 if x0 < 0: x0 = 0 if x1 > self.last_frame.shape[1]: x1 = self.last_frame.shape[1] if y0 < 0 : y0 = 0 if y1 > self.last_frame.shape[0]: y1 = self.last_frame.shape[0] szukaj.append([self.last_frame[y0:y1,x0: x1],[x0,y0,x1,y1]]) miss.append(mis) else: mis = self.last_frame2[(im[0] + int(self.y * (frame.shape[0] / 400))) * 2: (im[2] + int( self.y * (frame.shape[0] / 400))) * 2, (im[1] + int(self.x * (frame.shape[1] / 630))) * 2: (im[ 3] + int( self.x * (frame.shape[1] / 630))) * 2] miss.append(mis) x0 = (im[1] + int(self.x * (frame.shape[1] / 630))) * 2 - 30 x1 = (im[3] + int(self.x * (frame.shape[1] / 630))) * 2 + 30 y0 = (im[0] + int(self.y * (frame.shape[0] / 400))) * 2 - 15 y1 = (im[2] + int(self.y * (frame.shape[0] / 400))) * 2 + 15 if x0 < 0: x0 = 0 if x1 > self.last_frame.shape[1]: x1 = self.last_frame.shape[1] if y0 < 0 : y0 = 0 if y1 > self.last_frame.shape[0]: y1 = self.last_frame.shape[0] szukaj.append([self.last_frame[y0:y1,x0: x1],[x0,y0,x1,y1]]) #cv2.waitKey(0) finded = [] for mis in range(0,len(miss)): FLANN_INDEX_KDITREE = 0 MIN_MATCH_COUNT = 20 flannParam = dict(algorithm=FLANN_INDEX_KDITREE, tree=5) flann = cv2.FlannBasedMatcher(flannParam, {}) missa = cv2.cvtColor(miss[mis], cv2.COLOR_BGR2GRAY) szukaja = cv2.cvtColor(szukaj[mis][0], cv2.COLOR_BGR2GRAY) trainKP, trainDesc = sift.detectAndCompute(missa, None) queryKP, queryDesc = sift.detectAndCompute(szukaja, None) try: if (type(queryDesc) != 'NoneType') or (type(trainDesc) != 'NoneType') : matches = flann.knnMatch(queryDesc, trainDesc, k=2) goodMatch = [] for m, n in matches: if (m.distance < 0.75 * n.distance): goodMatch.append(m) if (len(goodMatch) > MIN_MATCH_COUNT): tp = [] qp = [] for m in goodMatch: tp.append(trainKP[m.trainIdx].pt) qp.append(queryKP[m.queryIdx].pt) tp, qp = np.float32((tp, qp)) H, status = cv2.findHomography(tp, qp, cv2.RANSAC, 3.0) h, w = missa.shape trainBorder = np.float32([[[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]]) queryBorder = cv2.perspectiveTransform(trainBorder, H) xy = [int(queryBorder[0][0][0]), int(queryBorder[0][0][1]), int(queryBorder[0][2][0]), int(queryBorder[0][2][1])] tabb = [szukaj[mis][1][0]+xy[0],szukaj[mis][1][0]+xy[2],szukaj[mis][1][1]+xy[1],szukaj[mis][1][1]+xy[3]] finded.append(tabb) else: print("Not Enough match found- %d/%d" % (len(goodMatch), MIN_MATCH_COUNT)) except: pass for find in finded: if find[0]<0: find[0] = 0 if find[1]<0: find[1] = 0 if find[2]<0: find[2] = 0 if find[3]<0: find[3] = 0 if find[0]>self.last_frame.shape[1]: find[0] = self.last_frame.shape[1] if find[1]>self.last_frame.shape[1]: find[1] = self.last_frame.shape[1] if find[2]>self.last_frame.shape[0]: find[2] = self.last_frame.shape[0] if find[3]>self.last_frame.shape[0]: find[3] = self.last_frame.shape[0] if find[2]>find[3]: temp = find[2] find[2]=find[3] find[3]=temp if find[0]>find[1]: temp = find[0] find[0]=find[1] find[1]=temp if find[2] == find[3]: find[2]=0 if find[0] == find[1]: find[0]=0 print(find[0]) print(find[1]) print(find[2]) print(find[3]) cand = self.last_frame[find[2] : find[3],find[0]: find[1] ] chars = self.Extractor.segment(cand) plate_numbers = [] number = "" for char in chars: plate_numbers.append(self.predict_character(char)) plate_numbers, ok = self.syntactic.check(plate_numbers) for char in plate_numbers: if ok == 2: number = number + self.class_names[char] if len(number) >= 2: found = 1 numbers.append(number) tablice.append([int((find[2]-int(self.y*(frame.shape[0]/400)))/2),int((find[0]-int(self.x*(frame.shape[1]/630))) / 2),int((find[3]-int(self.y*(frame.shape[0]/400)))/2),int((find[1]-int(self.x*(frame.shape[1]/630))) / 2)]) if found == 1: obiekt, text, rect, missing = self.tracker.update(tablice, numbers) for (objectID, centroid) in obiekt.items(): txt = "{}".format(text.get(objectID)) cv2.putText(frame, txt, (centroid[0]+int(self.x*(frame.shape[1]/630)) - 10, centroid[1]+int(self.y*(frame.shape[0]/400)) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) h, w, ch = rgbImage.shape bytesPerLine = ch * w p = QtGui.QImage(rgbImage.data, w, h, bytesPerLine, QtGui.QImage.Format_RGB888) p = QPixmap.fromImage(p) pixmap = p.scaled(630, 400) self.pix.emit(pixmap) fps.update() self.last_frame2 = self.last_frame fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
def start_detecting(self, arduinoConnector, threadID): print("ImageProcessor_start_detecting") #vs = PiVideoStream(resolution=(640, 480)).start() vs = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() while True: image = vs.read() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) print("Thread captured image %s" % threadID) # detect edges in the image # threshold the image by setting all pixel values less than 225 # to 255 (white; foreground) and all pixel values >= 225 to 255 # (black; background), thereby segmenting the image edged_image = cv2.Canny(gray, 224, 255) self.imageDebugger.debugImage("Edge", edged_image) # find contours (i.e., outlines) of the foreground objects in the # thresholded image cnts = cv2.findContours(edged_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[1] output = image.copy() # loop over the contours for c in cnts: # draw each contour on the output image with a 3px thick purple # outline, then display the output contours one at a time rect = cv2.boundingRect(c) x, y, w, h = rect # Sort out small elements if w < 50 or h < 50: cv2.drawContours(output, [c], -1, (240, 0, 159), 3) cv2.rectangle(output, (x, y), (x + w, y + h), (0, 0, 255), 2) continue # Sort out contours, which have a high difference between height and width # heigthWidthRatio = h/w # if (heigthWidthRatio < 0.5) | (heigthWidthRatio > 1.5): # cv2.drawContours(output, [c], -1, (240, 0, 159), 3) # cv2.rectangle(output, (x,y), (x+w, y+h), (0,50,210), 2) # continue # Resize image for DNN-Prediction im_cutted = gray[y - 5:(y + h + 5), x - 5:(x + w + 5)].copy() im_cutted_and_inverted = cv2.threshold(im_cutted.copy(), 100, 255, cv2.THRESH_BINARY_INV)[1] # If something went wrong in the thresholding function: continue if im_cutted_and_inverted is None: cv2.drawContours(output, [c], -1, (240, 0, 159), 3) cv2.rectangle(output, (x, y), (x + w, y + h), (0, 0, 255), 2) continue im_resize1 = cv2.resize(im_cutted_and_inverted, (28, 28)) im_resize2 = resize(im_resize1, (28, 28), mode='constant') im_final = im_resize2.reshape(1, 28, 28, 1) self.imageDebugger.debugImage("Resize", im_resize2) # Predict digit on image start_predict = time.time() ans = self.model.predict(im_final) time_for_predict = time.time() - start_predict number = ans[0].tolist().index(max(ans[0].tolist())) prob = ans[0].tolist()[number] * 100 if prob < 95: cv2.drawContours(output, [c], -1, (240, 0, 159), 3) cv2.rectangle(output, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.putText(output, ("%i" % number), (x - 20, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) cv2.putText(output, ("%.3f %%" % prob), (x + 50, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) continue print(ans.shape) # cv2.waitKey() # print(ans[0]) # print('DNN predicted digit is: ',ans) # print(cv2.contourArea(c)) # Print predicted digit cv2.drawContours(output, [c], -1, (240, 0, 159), 3) cv2.rectangle(output, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(output, ("%i" % number), (x - 20, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.putText(output, ("%.3f %%" % prob), (x + 50, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) # cv2.waitKey() self.imageDebugger.debugImage("Contours", output) # if q is pressed, break the loop key = cv2.waitKey(1) & 0xFF if key == ord("q"): break fps.update() fps.stop() vs.stop() cv2.destroyAllWindows()
def start(): # load the known faces and embeddings along with OpenCV's Haar # cascade for face detection print("[INFO] loading encodings + face detector...") data = pickle.loads(open(encodin, "rb").read()) detector = cv2.CascadeClassifier(cascade) # initialize the video stream and allow the camera sensor to warm up print("[INFO] starting video stream...") vs = VideoStream(src=0).start() # vs = VideoStream(usePiCamera=True).start() time.sleep(2.0) # start the FPS counter fps = FPS().start() # loop over frames from the video file stream n = 0 name = "" # must be empty while n < 10: # grab the frame from the threaded video stream and resize it # to 500px (to speedup processing) frame = vs.read() frame = imutils.resize(frame, width=500) # convert the input frame from (1) BGR to grayscale (for face # detection) and (2) from BGR to RGB (for face recognition) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # detect faces in the grayscale frame rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # OpenCV returns bounding box coordinates in (x, y, w, h) order # but we need them in (top, right, bottom, left) order, so we # need to do a bit of reordering boxes = [(y, x + w, y + h, x) for (x, y, w, h) in rects] # compute the facial embeddings for each face bounding box encodings = face_recognition.face_encodings(rgb, boxes) names = [] # loop over the facial embeddings for encoding in encodings: # attempt to match each face in the input image to our known # encodings matches = face_recognition.compare_faces(data["encodings"], encoding) name = "Unknown" # check to see if we have found a match if True in matches: # find the indexes of all matched faces then initialize a # dictionary to count the total number of times each face # was matched matchedIdxs = [i for (i, b) in enumerate(matches) if b] counts = {} # loop over the matched indexes and maintain a count for # each recognized face face for i in matchedIdxs: name = data["names"][i] counts[name] = counts.get(name, 0) + 1 # determine the recognized face with the largest number # of votes (note: in the event of an unlikely tie Python # will select first entry in the dictionary) name = max(counts, key=counts.get) # update the list of names names.append(name) # loop over the recognized faces for ((top, right, bottom, left), name) in zip(boxes, names): # draw the predicted face name on the image cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) y = top - 15 if top - 15 > 15 else top + 15 cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) # display the image to our screen cv2.imshow("Frame", 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() n = n + 1 # 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 name
class App: def __init__(self, logger, src, ROOT_DIR): self.vs = WebcamVideoStream(src) self.fps = FPS() self.logger = logger self.ROOT_DIR = ROOT_DIR cv2.namedWindow("Webcam") cv2.namedWindow("roi") cv2.namedWindow("stacked") cv2.createTrackbar('dilate kernel', 'roi', 3, 5, self.none) cv2.createTrackbar('erode kernel', 'roi', 2, 5, self.none) cv2.createTrackbar('blackhat kernel', 'roi', 21, 30, self.none) self.mouse = Mouse(window="Webcam") self.gt = Graphics() # self.hist = Hist() self.msg = "draw a rectangle to continue ..." self.font_20 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Light.ttf', 20) self.font_10 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Light.ttf', 15) self.font_30 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Light.ttf', 30) self.font_40 = ImageFont.truetype(f'{self.ROOT_DIR}/fonts/raleway/Raleway-Medium.ttf', 50) # self.stabilizer = VidStab() self.predictor = parser_v3.Predictor(model_path=f'{self.ROOT_DIR}/models/model_0.1v7.h5', root_dir=self.ROOT_DIR) def run(self): self.vs.start() self.fps.start() self.logger.info("app started ...") frame = self.vs.read() wh, ww, _ = frame.shape cv2.moveWindow("Webcam", 0, 0) cv2.moveWindow("roi", ww, 0) cv2.moveWindow("stacked", 0, wh + 79) self.mouse.rect = ((200, 200), (ww - 200, wh - 200)) while True: frame = self.vs.read() # frame = frame[:, ::-1] # flip orig = frame.copy() # stabilized_frame = self.stabilizer.stabilize_frame( # input_frame=frame, smoothing_window=30, border_size=50) if self.mouse.rect: p1, p2 = self.mouse.rect roi = self.gt.draw_roi(orig, p1, p2) if roi.size != 0: gray_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) dk = cv2.getTrackbarPos("dilate kernel", "roi") ek = cv2.getTrackbarPos("erode kernel", "roi") bk = cv2.getTrackbarPos("blackhat kernel", "roi") thresh = im.thresify(gray_roi, dk, ek, bk) # print(thresh.shape) # overlap thresh on original image mod_thres = cv2.bitwise_not(thresh) #orig[p1[1]:p2[1], p1[0]:p2[0]] = cv2.merge((mod_thres, mod_thres, mod_thres)) # boxes, digits, cnts = component.get_symbols( # thresh, im=roi, draw_contours=True) digits, boxes = component.connect_cnts2(thresh, roi) stacked_digits, resized_digits = component.stack_digits(digits, im.resize) res, latex_image, labels = self.predictor.parse(resized_digits, boxes) # annotate symbols # for label, (pre_label, x_min, y_min, x_max, y_max) in zip(labels, boxes): # self.gt.write(orig, label, (x_min, y_min), self.font_20) # res = self.predictor.parse(resized_digits, boxes) #print(f"res: {res}") self.gt.write(orig, res, (10, wh - wh / 8), self.font_10) # self.gt.draw_boxes(orig, boxes, p1, p2) # self.hist.draw(gray_roi) cv2.imshow('roi', thresh) cv2.imshow('stacked', stacked_digits) # cv2.imshow('latex_image', latex_image) # self.gt.write(orig, self.msg, (10, 10), self.font_20) else: orig[:, :] = cv2.blur(orig, (100, 100)) windowRect = cv2.getWindowImageRect('Webcam') wx, wy, ww, wh = windowRect self.gt.write(orig, self.msg, (100, wh / 2 - 30), self.font_30) cv2.imshow('Webcam', orig) # cv2.imshow('stab', stabilized_frame) self.fps.update() key = cv2.waitKey(1) if (key & 0xFF == ord('q')) or (key & 0xFF == 27): self.logger.info('exiting ...') break elif key & 0xFF == ord('c'): CAPTURED_DIR = f'{self.ROOT_DIR}/screenshots' imageName = f'{CAPTURED_DIR}/{str(time.strftime("%Y_%m_%d_%H_%M"))}.png' cv2.imwrite(imageName, orig) self.logger.info(f'screenshot saved at {imageName}') self.fps.stop() self.vs.stop() cv2.destroyAllWindows() self.logger.info("elapsed time: {:.2f}".format(self.fps.elapsed())) self.logger.info("approx. FPS: {:.2f}".format(self.fps.fps())) def none(self, x): pass
def object_frame(): interpreter = make_interpreter(model_file) interpreter.allocate_tensors() gst_str = ( 'udpsrc port=5600 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264"' # ' ! rtpjitterbuffer' ' ! queue' ' ! parsebin' ' ! decodebin' ' ! videoconvert' ' ! appsink emit-signals=true sync=false max-buffers=1 drop=true') cap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) # cap = cv2.VideoCapture('./video/IA_video.avi') # landscape width = 640 height = 360 fileOut = cv2.VideoWriter('dji.avi', cv2.VideoWriter_fourcc(*'XVID'), 16, (width, height)) state = 'fly' labeltxt = None global y # keep looping fps = FPS().start() while True: ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) imag = cv2.resize(frame, (300, 300)) common.set_input(interpreter, imag) interpreter.invoke() scale = (1, 1) objects = detect.get_objects(interpreter, confThreshold, scale) # print('objects:',objects) data_out = [] if objects: for obj in objects: inference = [] # clear inference box = obj.bbox inference.extend((obj.id, obj.score, box)) # print('inference:',inference) data_out.append(inference) # list of all detected objects # print('data_out:',data_out) objID = data_out[0][0] # object with largest confidence selected confidence = data_out[0][1] labeltxt = labels[objID] box = data_out[0][2] if confidence > confThreshold: left, right, top, bottom = draw_rect( imag, box, confidence, labeltxt, use_normalized_coordinates=False) else: left, right, top, bottom = [0, 0, 0, 0] if labeltxt == 'person': w = int(right - left) h = int(bottom - top) # print('h,w:',h,w) if state == 'avoid': # if left > 100: # print('go left') # else: # print('go right') if h < 135: state = 'fly' event.clear() else: if h > 150: state = 'avoid' event.set() if left > 100: y = -0.8 print('go left') else: y = 0.8 print('go right') outputDet = cv2.resize(imag, (width, height)) outputDet = cv2.cvtColor(outputDet, cv2.COLOR_RGB2BGR) cv2.putText(outputDet, state, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imshow('frame', outputDet) fileOut.write(outputDet) fps.update() k = cv2.waitKey(30) & 0xff # press ESC to exit if k == 27: break fps.stop() fileOut.release() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
def main(config): """Video verticalizer main script.""" # SET CONFIG VARIABLES canvas_shape = [ int(config['CONFIG']['canvas_height']), int(config['CONFIG']['canvas_width']), 3 ] crop_ratio = float(config['CONFIG']['crop_ratio']) video_root = str(config['CONFIG']['video_root']) final_width = int(config['CONFIG']['final_width']) final_height = int(config['CONFIG']['final_height']) output_root = str(config['CONFIG']['output_root']) output_format = str(config['CONFIG']['output_format']) show_progress = True if config['CONFIG']['show_progress'] in ['True', 1 ] else False H_CROP_SIZE = int(canvas_shape[0] * crop_ratio) // 2 W_CROP_SIZE = int(canvas_shape[1] * crop_ratio) // 2 movie_dict = config.sections()[1:] # Removes the 'CONFIG' section. try: for movie in movie_dict: movie_filename = str(config[movie]['filename']) start_time = float(config[movie]['start_time']) end_time = float(config[movie]['end_time']) video_file = os.path.join(video_root, movie_filename) assert os.path.exists( video_file), f"Video does not exist at {video_file}" output_dir = output_root + f'_{crop_ratio}' os.makedirs(output_dir, exist_ok=True) save_filename = f"{os.path.basename(video_file).split('.')[0]}_{crop_ratio}{output_format}" save_name = os.path.join(output_dir, save_filename) if os.path.exists(save_name): continue cap = cv2.VideoCapture(video_file) fps = FPS().start() frames_per_second = int(cap.get(cv2.CAP_PROP_FPS)) total_number_of_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if end_time == -1: end_time = total_number_of_frames / frames_per_second start_time_ms = start_time * 1000 end_time_ms = end_time * 1000 movie_time = end_time_ms - start_time_ms duration = movie_time / 1000 start_frame = int((start_time_ms / 1000) * frames_per_second) number_of_frames = int((movie_time / 1000) * frames_per_second) frame_skip = int(np.ceil(number_of_frames / canvas_shape[1])) frames_to_take = int(number_of_frames // frame_skip) canvas = np.ones([canvas_shape[0], 1, 3]).astype(np.uint8) line_width = 1 new_line = np.ones([canvas_shape[0], line_width, 3]).astype(np.uint8) print(f'movie: {movie}') print(f'vid_length: {duration}') print(f'frames_per_second: {frames_per_second}') print(f'num_frames: {number_of_frames}') print(f'frames_to_take: {frames_to_take}') print(f'frame_skip: {frame_skip}') print( f'output_shape: [{canvas.shape[0]}, {frames_to_take}, {canvas.shape[2]}]' ) force_quit = False for frame_idx in tqdm(range(start_frame, number_of_frames, frame_skip), desc='Frames'): if cap.get(cv2.CAP_PROP_POS_MSEC) >= end_time_ms: # END OF MOVIE REACHED break cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx) grabbed, frame = cap.read() if not grabbed: # COULD NOT GET NEXT FRAME (END OF FILE) break if frame is not None: h, w, c = frame.shape frame = frame[h // 2 - H_CROP_SIZE:h // 2 + H_CROP_SIZE, w // 2 - W_CROP_SIZE:w // 2 + W_CROP_SIZE] average_color = np.mean(frame, (0, 1)).astype(np.uint8) this_line = np.array(new_line * average_color).astype( np.uint8) canvas = np.hstack([canvas, this_line]) if show_progress: cv2.imshow('frame', canvas) if cv2.waitKey(1) & 0xFF == ord('q'): force_quit = True break fps.update() if not force_quit: RGBimage = cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB) UpscaleRGBimage = upscale(RGBimage, final_width, final_height) PILimage = Image.fromarray(UpscaleRGBimage) print(f'Output: {save_name}\n') PILimage.save(save_name) except KeyboardInterrupt: print('Stopped by user')
def _main_(): config_path = 'ncsmodel/config.json' image_path = 'videos/uba.mp4' keras.backend.tensorflow_backend.set_session(get_session()) with open(config_path) as config_buffer: config = json.load(config_buffer) ############################### # Make the model ############################### input_size = (config['model']['input_size_h'], config['model']['input_size_w']) labels = config['model']['labels'] max_box_per_image = config['model']['max_box_per_image'] anchors = config['model']['anchors'] gray_mode = config['model']['gray_mode'] nb_class = 1 # Net Config NN = Net(load_weights=True) cv2_image = cv2.imread("images/person.jpg", 0) image = NN.image if config['model']['gray_mode']: depth = 1 else: depth = 3 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) if image_path[-4:] == '.mp4': video_out = image_path[:-4] + '_detected' + image_path[-4:] cap = FileVideoStream(image_path).start() time.sleep(1.0) fps = FPS().start() fps_img = 0.0 counter = 0 tf_image = NN.image while True: start = time.time() image = cap.read() print(image.min(), image.max(), image.mean(), image.shape) if depth == 1: # convert video to gray gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # resize for plotting resized_image = cv2.resize(gray_image, input_size, interpolation=cv2.INTER_CUBIC) # convert to float image_data = np.array(resized_image, dtype=np.float16) image_data /= 255. # dimensions for inference (1, w, h, c) image_data = np.expand_dims(np.expand_dims(image_data, 0), 3) # 1, 256, 256, 1 # dimension for plot (w, h, c) plot_image = np.expand_dims(resized_image, 2) else: if counter == 1: print("Color image") image_data = cv2.resize(image, input_size, interpolation=cv2.INTER_CUBIC) #image = np.array(image, dtype='f') #image = np.divide(image, 255.) tm_inf = time.time() netout = sess.run(NN.predict(), feed_dict={tf_image: image_data}) netout = np.reshape(np.squeeze(netout, axis=0), (8, 8, 5, 6)) boxes = decode_netout(netout, anchors, nb_class) fps_img = (fps_img + (1 / (time.time() - start))) / 2 #print( "Inference time: {:.4f}".format(time.time() - tm_inf) ) print(plot_image.shape) image = draw_boxes(plot_image, boxes, labels) #image = cv2.putText(image, "fps: {:.2f}".format(fps_img), (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 4 ) cv2.imshow("Press q to quit", image) fps.update() #if counter == 10: #print(image.sum(), boxes) # time.sleep(1) counter += 1 if cv2.getWindowProperty("Press q to quit", cv2.WND_PROP_ASPECT_RATIO) < 0.0: print("Window closed") break elif cv2.waitKey(1) & 0xFF == ord('q'): print("Q pressed") break fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cap.release() else: images = list(list_images(image_path)) for fname in images[100:]: image = cv2.imread(fname) tm_inf = time.time() boxes = yolo.predict(image) print("Inference time: {:.4f}".format(time.time() - tm_inf)) image = draw_boxes(image, boxes, config['model']['labels']) cv2.imshow("Press q to quit", image) if cv2.waitKey(1) & 0xFF == ord('q'): break time.sleep(2) cv2.destroyAllWindows()
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) print label # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break # completely read file so break if args.get("video", None) is not None: if vs.more() is False: 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()
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
def analyse_video(path): pridictions_datas = {} pridictions_datas['hoodie'] = 'NotAvailable' terminate =False # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") vs = VideoStream(path).start() # video_capture = cv2.VideoCapture(path) time.sleep(0.0) fps = FPS().start() count = 0 # loop over the frames from the video stream face_Available = False while True and terminate is False: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels # frame = video_capture.read()[1] frame = vs.read() image = frame.copy() frame = imutils.resize(frame, width=1800) if not face_Available: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 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 >confidence_range: # extract the index of the class label from the # `detections` idx = int(detections[0, 0, i, 1]) # if the predicted class label is in the set of classes # we want to ignore then skip the detection if CLASSES[idx] in IGNORE: continue # compute the (x, y)-coordinates of the bounding box for # the object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # draw the 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) if confidence > 0.90: if checkFace(gray,frame): pridictions_datas["faceImagePath"] = str(imageName)+'.jpg' face_Available = True else: # image = image[y - 100:endY + 100, y:startX] with graph.as_default(): pridicted_labl, probability = checkAbnormalCloth(image) face_mask=False if pridicted_labl is 'Hoodies' and probability > 95 and checkFace(gray, frame) is None and face_mask: print(probability) pridictions_datas['abnormal_person'] = 'Available' terminate = True if face_Available: with graph.as_default(): emotion_analyse_data = emotion_analyse(path) print(emotion_analyse_data) max_key = max(emotion_analyse_data, key=lambda k: emotion_analyse_data[k]) if max_key is 'fear' or max_key is 'angry': pridictions_datas["Identified_emotion"] =max_key pridictions_datas["bg_color"] = '#FF0000' pridictions_datas["Alart_msg"] = 'The Person Pridicted as Criminal' else: pridictions_datas["Identified_emotion"] = max_key pridictions_datas["bg_color"] = '#228B22' pridictions_datas["Alart_msg"] = 'Normal Person' age_gen_pridiction_lable = age_gen_analyse(StaticDir+'/analyse_img/analyse_img.jpg') age = age_gen_pridiction_lable.split(',')[0] pridictions_datas["age"] = age pridictions_datas["gender"] = age_gen_pridiction_lable.split(',')[1] if int(age) >= 5 and int(age) <= 15: pridictions_datas["age_range"] = 'Children' if int(age) >= 16 and int(age) <= 25: pridictions_datas["age_range"] = 'Youth' if int(age) >= 26 and int(age) <= 40: pridictions_datas["age_range"] = 'Adults' if int(age) >= 41: pridictions_datas["age_range"] = 'Seniors' wrinkle_persetage=wrinkles_analyse(StaticDir+'/face/' + str(imageName) + '.jpg') pridictions_datas["wrinkle_persetage"] = str(wrinkle_persetage) face_analyse_data = face_analyse(StaticDir+'/face/' + str(imageName) + '.jpg') terminate = True fps.update() # stop the timer and display FPS information fps.stop() cv2.destroyAllWindows() vs.stop() return pridictions_datas , emotion_analyse_data ,face_analyse_data
def facialRec(): subprocess.call(['sh', './whiteLED.sh']) #Turn the LED white #Initialize 'currentname' to trigger only when a new person is identified. currentname = "unknown" #Determine faces from encodings.pickle file model created from train_model.py encodingsP = "encodings.pickle" #use this xml file cascade = "haarcascade_frontalface_default.xml" # load the known faces and embeddings along with OpenCV's Haar # cascade for face detection #print("[INFO] loading encodings + face detector...") data = pickle.loads(open(encodingsP, "rb").read()) detector = cv2.CascadeClassifier(cascade) # initialize the video stream and allow the camera sensor to warm up #print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) # start the FPS counter fps = FPS().start() valid = False user_recognized = False # setting the max time for this function to run to 2 mins end_time = datetime.datetime.now() + datetime.timedelta(minutes=2) while not (user_recognized): # grab the frame from the threaded video stream and resize it # to 500px (to speedup processing) frame = vs.read() frame = imutils.resize(frame, width=500) # convert the input frame from (1) BGR to grayscale (for face # detection) and (2) from BGR to RGB (for face recognition) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # detect faces in the grayscale frame rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) # OpenCV returns bounding box coordinates in (x, y, w, h) order # but we need them in (top, right, bottom, left) order, so we # need to do a bit of reordering boxes = [(y, x + w, y + h, x) for (x, y, w, h) in rects] # compute the facial embeddings for each face bounding box encodings = face_recognition.face_encodings(rgb, boxes) names = [] # loop over the facial embeddings for encoding in encodings: # attempt to match each face in the input image to our known # encodings matches = face_recognition.compare_faces(data["encodings"], encoding) name = "Unknown" #if face is not recognized, then print Unknown # check to see if we have found a match if True in matches: # find the indexes of all matched faces then initialize a # dictionary to count the total number of times each face # was matched matchedIdxs = [i for (i, b) in enumerate(matches) if b] counts = {} # loop over the matched indexes and maintain a count for # each recognized face face for i in matchedIdxs: name = data["names"][i] counts[name] = counts.get(name, 0) + 1 # determine the recognized face with the largest number # of votes (note: in the event of an unlikely tie Python # will select first entry in the dictionary) name = max(counts, key=counts.get) #If someone in your dataset is identified, print their name on the screen if currentname != name: currentname = name user_recognized = True print("The user is: " + currentname) if ((currentname == 'Moisess') or (currentname == 'Zach')): #set the valid bit to true valid = True new_end_time = datetime.datetime.now( ) + datetime.timedelta(seconds=30) GPIO.output( 6, 1 ) # Set the GPIO to the MSP430 to High, signaling the U$ while True: if (GPIO.input(12)): print( "Speech and face recognized, changing LEDs to green" ) subprocess.call(['sh', './greenLED.sh' ]) #Turn the LED Green break if datetime.datetime.now() >= new_end_time: print('inner time limit reached') break time.sleep(60) GPIO.output(6, 0) # if(GPIO.input(12) and valid == True): # print("Speech and face recognized, changing LEDs to green") # subprocess.call(['sh', './greenLED.sh']) #Turn the LED Green # GPIO.output(6, 1) # Set the GPIO to the MSP430 to High, signaling the User's Pin# was detected # time.sleep(240) # GPIO.output(6, 0) # elif(GPIO.input(12) or valid == True): # print("Speech Recognized or Face Recognized, changing LEDs to blue") # subprocess.call(['sh', './greenLED.sh']) #Turn the LED Yellow # time.sleep(3) # GPIO.output(6, 1) # Set the GPIO to the MSP430 to High, signaling the User's Pin# was detected # new_end_time = datetime.datetime.now() + datetime.timedelta(seconds=30) # while True: # if(GPIO.input(12) and valid == True): # print("Speech and face recognized, changing LEDs to green") # subprocess.call(['sh', './greenLED.sh']) #Turn the LED Green # break # if datetime.datetime.now() >= new_end_time: # print ('time limit reached') # break # time.sleep(60) # GPIO.output(6, 0) # elif(GPIO.input(12) or valid == True): # print("Face Recognized but not speech, changing LEDs to blue") #subprocess.call(['sh', './blueLED.sh']) #Turn the LED Blue # setting the max time for this function to run to 2 mins #new_end_time = datetime.datetime.now() + datetime.timedelta(seconds=30) #GPIO.output(6, 1) # Set the GPIO to the MSP430 to High, signaling the User's Pin# #while True: # if(GPIO.input(12) and valid == True): # print("Speech and face recognized, changing LEDs to green") # subprocess.call(['sh', './greenLED.sh']) #Turn the LED Green #if valid or datetime.datetime.now() >= newend_time: # print ('time limit reached') # break #time.sleep(5) #GPIO.output(6, 0) #break else: print('Unauthorized user!') subprocess.call(['sh', './redLED.sh']) #Turn the LED red time.sleep(2) time.sleep(1) # update the list of names names.append(name) # update the FPS counter fps.update() #if the user is identified or the time limit is reached then exit if valid or datetime.datetime.now() >= end_time: print('time limit reached') break # stop the timer and display FPS information fps.stop() print("Ending facialRec()...") subprocess.call(['sh', './offLED.sh']) #Turn the LED off # do a bit of cleanup cv2.destroyAllWindows() vs.stop() time.sleep(10)
# loop over some frames for (i, f) in enumerate(stream): # grab the frame from the stream and resize it to have a maximum # width of 400 pixels frame = f.array frame = imutils.resize(frame, width=400) # check to see if the frame should be displayed to our screen if args["display"] > 0: 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()
def detect(language: str, camera: str, path: str): """ Take feed from camera dn detect vehicle (using jetson-inference package), draw bounding box, and read license plate (based on the language) Parameters ---------- language: str Region of the license plate that OpenALPR will detect camera: str Specified camera input to use path: str Specified path to OpenALPR folder """ alpr = get_alpr(language, path) net = jetson.inference.detectNet("ssd-mobilenet-v1", threshold=0.5) print("Starting video stream...") if camera == "jetson_cam": cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER) else: cap = VideoCaptureThreading("/dev/video" + camera) cap.start() fps = FPS().start() while True: _, frame, oframe, x, imgz = cap.read() img = frame.copy() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA).astype(np.float32) img = jetson.utils.cudaFromNumpy(img) detections = net.Detect(img, 1280, 720) img = jetson.utils.cudaToNumpy(img, 1280, 720, 4) img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR).astype(np.uint8) for obj in detections: classid = obj.ClassID x1, y1, x2, y2 = [int(i) for i in obj.ROI] if classid in [3, 4, 6, 8]: cropped = frame[y1:y2, x1:x2] results = alpr.recognize_ndarray(cropped) frame = cv2.rectangle( frame, (int(x1), int(y1)), (int(x2), int(y2)), (36, 255, 12), 2 ) if len(results["results"]) == 0: continue else: plate = results["results"][0]["plate"] confidence = results["results"][0]["confidence"] cv2.putText( frame, plate + ": " + str(confidence), (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2, ) cv2.imshow("frame", frame) if cv2.waitKey(1) & 0xFF == ord("q"): cap.stop() break fps.update() fps.stop() print("Elapsed time: {:.2f}".format(fps.elapsed())) print("Approx. FPS: {:.2f}".format(fps.fps())) # clean up capture window # cap.release() cv2.destroyAllWindows()