def main(yolo): source = 'gst.jpg' # 0 or youtube or jpg URL = 'https://youtu.be/OI802VvUN38' FLAGScsv = 1 if FLAGScsv: csv_obj = save_csv() num_a2b, num_b2a = csv_obj.startday() #read old count from csv file else: num_a2b = 0 #start from zero num_b2a = 0 ina_old = set() ina_now = set() inb_old = set() inb_now = set() num_a2b_old = 0 num_b2a_old = 0 a2b_old = set() b2a_old = set() i = 0 points = [] tpro = 0. # Definition of the parameters max_cosine_distance = 0.1 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'model_data/mars-small128.pb' encoder = gdet.create_box_encoder(model_filename, batch_size=1) metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) if source == 'youtube': width = 854 height = 480 sp.call(["youtube-dl", "--list-format", URL]) run = sp.Popen(["youtube-dl", "-f", "94", "-g", URL], stdout=sp.PIPE) VIDM3U8, _ = run.communicate() VIDM3U8 = str(VIDM3U8, 'utf-8') VIDM3U8 = "".join(("hls://", str(VIDM3U8))) p1 = sp.Popen([ 'streamlink', '--hls-segment-threads', '10', VIDM3U8, 'best', '-o', '-' ], stdout=sp.PIPE) p2 = sp.Popen([ 'ffmpeg', '-i', '-', '-f', 'image2pipe', "-loglevel", "quiet", "-pix_fmt", "bgr24", "-vcodec", "rawvideo", '-r', '10', "-" ], stdin=p1.stdout, stdout=sp.PIPE) else: video_capture = cv2.VideoCapture(source) print('video source : ', source) out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (608, 608)) # ___________________________________________________________________________________________________________________________________________MAIN LOOP while True: # get 1 frame if source == 'youtube': raw_frame = p2.stdout.read(width * height * 3) frame = np.fromstring(raw_frame, dtype='uint8').reshape( (height, width, 3)) elif source == 'gst.jpg': try: img_bin = open('gst.jpg', 'rb') buff = io.BytesIO() buff.write(img_bin.read()) buff.seek(0) temp_img = numpy.array(Image.open(buff), dtype=numpy.uint8) frame = cv2.cvtColor(temp_img, cv2.COLOR_RGB2BGR) except OSError: continue except TypeError: continue else: ret, frame = video_capture.read() if ret != True: break image = Image.fromarray(frame) # ______________________________________________________________________________________________________________________________DETECT WITH YOLO t1 = time.time() boxs = yolo.detect_image(image) # print("box_num",len(boxs)) features = encoder(frame, boxs) # score to 1.0 here). detections = [ Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features) ] # Run non-max suppression. boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) indices = preprocessing.non_max_suppression( boxes, nms_max_overlap, scores) #index that filtered detections = [detections[i] for i in indices] # ______________________________________________________________________________________________________________________________DRAW DETECT BOX for det in detections: bbox = det.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 0, 255), 1) # ___________________________________________________________________________Call the tracker tracker.predict() tracker.update(detections) # __________________________________________________________________________________________________________________________DRAW TRACK RECTANGLE ina_now = set() inb_now = set() for track in tracker.tracks: if track.is_confirmed() and track.time_since_update > 1: continue bbox = track.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1]) + 30), cv2.FONT_HERSHEY_SIMPLEX, 5e-3 * 200, (0, 255, 0), 3) dot = int(int(bbox[0]) + ((int(bbox[2]) - int(bbox[0])) / 2)), int(bbox[3] - 5) cv2.circle(frame, dot, 10, (0, 0, 255), -1) if points: dotc = Point(dot) ina_now.add(track.track_id) if ( polygon_a.contains(dotc) and track.track_id not in ina_now) else None inb_now.add(track.track_id) if ( polygon_b.contains(dotc) and track.track_id not in inb_now) else None # print('ina_now : ',ina_now,'ina_old : ',ina_old) for item in a2b: #check who pass a->b is already exist in a2b_cus a2b_cus.add(item) if item not in a2b_cus else None a2b = inb_now.intersection(ina_old) num_a2b += len(a2b - a2b_old) b2a = ina_now.intersection(inb_old) num_b2a += len(b2a - b2a_old) a2b_old = a2b b2a_old = b2a ina_old = ina_old.union(ina_now) inb_old = inb_old.union(inb_now) i += 1 if i > 10: #slow down backup old ina_old.pop() if len(ina_old) != 0 else None inb_old.pop() if len(inb_old) != 0 else None i = 0 # print('num_a2b : ',num_a2b,'num_b2a : ',num_b2a) # __________________________________________________________________________________________________________________________________________CSV if FLAGScsv and ((num_a2b_old != num_a2b) or (num_b2a_old != num_b2a)): record = [ time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), num_a2b, num_b2a, num_a2b - num_b2a ] csv_obj.save_this(record) num_a2b_old = num_a2b num_b2a_old = num_b2a # _________________________________________________________________________________________________________________________GET POINTS From click if (cv2.waitKey(1) == ord('p')): points = get_lines.run(frame, multi=True) print(points) #region if len(points) % 3 == 0 and len(points) / 3 == 1: #1 door print('1 door mode') polygon_a = Polygon([ points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4] ]) polygon_b = Polygon([ points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4] ]) elif len(points) % 3 == 0 and len(points) / 3 == 2: #2 door print('2 doors mode') polygon_a1 = Polygon([ points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4] ]) polygon_a2 = Polygon([ points[3][0:2], points[3][2:4], points[4][0:2], points[4][2:4] ]) polygon_a = [polygon_a1, polygon_a2] polygon_a = cascaded_union(polygon_a) polygon_b1 = Polygon([ points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4] ]) polygon_b2 = Polygon([ points[4][0:2], points[4][2:4], points[5][0:2], points[5][2:4] ]) polygon_b = [polygon_b1, polygon_b2] polygon_b = cascaded_union(polygon_b) else: print('Please draw 3 or 6 lines') break if points: for line in points: cv2.line(frame, line[0:2], line[2:4], (0, 255, 255), 2) # draw line cv2.putText(frame, 'in : ' + str(num_a2b), (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, 'out : ' + str(num_b2a), (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA) out.write(frame) cv2.imshow('', frame) print('process time : ', time.time() - tpro) tpro = time.time() if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() out.release() cv2.destroyAllWindows()
def main(yolo): source = 0 # 0 for webcam or youtube or jpg FLAGScsv = 1 if FLAGScsv: csv_obj = save_csv() num_a2b_start, num_b2a_start = csv_obj.startday( ) #read old count from csv file else: num_a2b_start = 0 #start from zero num_b2a_start = 0 ina_old = set() ina_now = set() inb_old = set() inb_now = set() num_a2b_old = 0 num_b2a_old = 0 a2b_old = set() b2a_old = set() i = 0 a2b_cus = set() b2a_cus = set() #points=[(462, 259, 608, 608), (439, 608, 387, 403), (279, 456, 285, 608), (182, 70, 249, 168), (218, 278, 116, 95), (60, 166, 235, 331)] with open('linefile', 'rb') as fp: points = pickle.load(fp) print('Load lines :', points) if points: if len(points) % 3 == 0 and len(points) / 3 == 1: #1 door print('1 door mode') polygon_a = Polygon([ points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4] ]) polygon_b = Polygon([ points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4] ]) elif len(points) % 3 == 0 and len(points) / 3 == 2: #2 door print('2 doors mode') polygon_a1 = Polygon([ points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4] ]) polygon_a2 = Polygon([ points[3][0:2], points[3][2:4], points[4][0:2], points[4][2:4] ]) polygon_a = [polygon_a1, polygon_a2] polygon_a = cascaded_union(polygon_a) polygon_b1 = Polygon([ points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4] ]) polygon_b2 = Polygon([ points[4][0:2], points[4][2:4], points[5][0:2], points[5][2:4] ]) polygon_b = [polygon_b1, polygon_b2] polygon_b = cascaded_union(polygon_b) tpro = 0. # Definition of the parameters max_cosine_distance = 0.7 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'model_data/mars-small128.pb' encoder = gdet.create_box_encoder(model_filename, batch_size=1) metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) video_capture = cv2.VideoCapture(source) print('video source : ', source) #out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (608,608)) # ___________________________________________________________________________________________________________________________________________MAIN LOOP while True: # get 1 frame if source == 'youtube': raw_frame = p2.stdout.read(width * height * 3) frame = np.fromstring(raw_frame, dtype='uint8').reshape( (width, height, 3)) elif source == 'gst.jpg': try: img_bin = open('gst.jpg', 'rb') buff = io.BytesIO() buff.write(img_bin.read()) buff.seek(0) frame = numpy.array(Image.open(buff), dtype=numpy.uint8) #RGB #frame=adjust_gamma(frame,gamma=1.6) frame = cv2.resize(frame, (608, 608)) except OSError: continue except TypeError: continue else: ret, frame = video_capture.read() frame = cv2.resize( frame, (608, 608)) # maybe your webcam is not in the right size frame = cv2.cvtColor( frame, cv2.COLOR_RGB2BGR) # because opencv read as BGR if ret != True: break image = Image.fromarray(frame) # ______________________________________________________________________________________________________________________________DETECT WITH YOLO t1 = time.time() boxs = yolo.detect_image(image) # print("box_num",len(boxs)) features = encoder(frame, boxs) # score to 1.0 here). detections = [ Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features) ] # Run non-max suppression. boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) indices = preprocessing.non_max_suppression( boxes, nms_max_overlap, scores) #index that filtered detections = [detections[i] for i in indices] # ______________________________________________________________________________________________________________________________DRAW DETECT BOX for det in detections: bbox = det.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 0, 255), 1) # ___________________________________________________________________________Call the tracker tracker.predict() tracker.update(detections) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) #change to BGR for show only # __________________________________________________________________________________________________________________________DRAW TRACK RECTANGLE ina_now = set() inb_now = set() for track in tracker.tracks: if track.is_confirmed() and track.time_since_update > 1: continue bbox = track.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1]) + 30), cv2.FONT_HERSHEY_SIMPLEX, 5e-3 * 200, (0, 255, 0), 3) dot = int(int(bbox[0]) + ((int(bbox[2]) - int(bbox[0])) / 2)), int(bbox[3] - 15) cv2.circle(frame, dot, 10, (0, 0, 255), -1) if points: dotc = Point(dot) ina_now.add(track.track_id) if ( polygon_a.contains(dotc) and track.track_id not in ina_now) else None inb_now.add(track.track_id) if ( polygon_b.contains(dotc) and track.track_id not in inb_now) else None # print('ina_now : ',ina_now,'ina_old : ',ina_old) # print('inb_now : ',inb_now,'inb_old : ',inb_old) a2b = inb_now.intersection(ina_old) for item in a2b: #check who pass a->b is already exist in a2b_cus a2b_cus.add(item) if item not in a2b_cus else None num_a2b = num_a2b_start + len(a2b_cus) b2a = ina_now.intersection(inb_old) for item in b2a: #check who pass a->b is already exist in a2b_cus b2a_cus.add(item) if item not in b2a_cus else None num_b2a = num_b2a_start + len(b2a_cus) a2b_old = a2b b2a_old = b2a ina_old = ina_now inb_old = inb_now # i+=1 # if i > 30 : #slow down backup old # ina_old =set() # inb_old =set() # i=0 # __________________________________________________________________________________________________________________CSV if FLAGScsv and ((num_a2b_old != num_a2b) or (num_b2a_old != num_b2a)): record = [ time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()), num_a2b, num_b2a, num_a2b - num_b2a ] csv_obj.save_this(record) num_a2b_old = num_a2b num_b2a_old = num_b2a # _____________________________________________________________________________________________________GET POINTS From click if (cv2.waitKey(1) == ord('p')): points = get_lines.run(frame, multi=True) print(points) #region if len(points) % 3 == 0 and len(points) / 3 == 1: #1 door print('1 door mode') polygon_a = Polygon([ points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4] ]) polygon_b = Polygon([ points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4] ]) #save to file with open('linefile', 'wb') as fp: pickle.dump(points, fp) elif len(points) % 3 == 0 and len(points) / 3 == 2: #2 door print('2 doors mode') polygon_a1 = Polygon([ points[0][0:2], points[0][2:4], points[1][0:2], points[1][2:4] ]) polygon_a2 = Polygon([ points[3][0:2], points[3][2:4], points[4][0:2], points[4][2:4] ]) polygon_a = [polygon_a1, polygon_a2] polygon_a = cascaded_union(polygon_a) polygon_b1 = Polygon([ points[1][0:2], points[1][2:4], points[2][0:2], points[2][2:4] ]) polygon_b2 = Polygon([ points[4][0:2], points[4][2:4], points[5][0:2], points[5][2:4] ]) polygon_b = [polygon_b1, polygon_b2] polygon_b = cascaded_union(polygon_b) with open('linefile', 'wb') as fp: pickle.dump(points, fp) else: print('Please draw 3 or 6 lines') break if points: for line in points: cv2.line(frame, line[0:2], line[2:4], (0, 255, 255), 2) # draw line cv2.putText(frame, 'in : ' + str(num_a2b), (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA) cv2.putText(frame, 'out : ' + str(num_b2a), (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA) #out.write(frame) # cv2.imshow('', frame) print('process time : ', time.time() - tpro) tpro = time.time() if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() #out.release() cv2.destroyAllWindows()
def main(yolo): points=[] tpro=0. # Definition of the parameters max_cosine_distance = 0.9 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'model_data/mars-small128.pb' encoder = gdet.create_box_encoder(model_filename,batch_size=1) metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) writeVideo_flag = True video_capture = cv2.VideoCapture(0) if writeVideo_flag: # Define the codec and create VideoWriter object w = int(video_capture.get(3)) h = int(video_capture.get(4)) fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('output.avi', fourcc, 15, (w, h)) list_file = open('detection.txt', 'w') frame_index = -1 fps = 0.0 while True: ret, frame = video_capture.read() # frame shape 640*480*3 if ret != True: break; frame=cv2.flip(frame,1) image = Image.fromarray(frame) # ___________________________________________________________________________DETECT WITH YOLO t1 = time.time() boxs = yolo.detect_image(image) # print("box_num",len(boxs)) features = encoder(frame,boxs) # score to 1.0 here). detections = [Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features)] # Run non-maxima suppression. boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores) detections = [detections[i] for i in indices] # ___________________________________________________________________________DRAW DETECT BOX to_move=[] for det in detections: bbox = det.to_tlbr() cv2.rectangle(frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(0,0,255), 1) temp=int(bbox[0]),int(bbox[1]),int(bbox[2]),int(bbox[3]) to_move.append(temp) # now feed tracked box to move # ___________________________________________________________________________MOVE if to_move : # Initial co-ordinates of the object to be tracked # Create the tracker object mover = [dlib.correlation_tracker() for _ in range(len(to_move))] # Provide the tracker the initial position of the object [mover[i].start_track(frame, dlib.rectangle(*rect)) for i, rect in enumerate(to_move)] ## FEED FIRST BOX HERE for i in range (0,100): ##### START LOOP MOVER ret, frame = video_capture.read() # tempo full_frame_mover=[] frame=cv2.flip(frame,1) #tempo # Update the mover for i in range(len(mover)): #_____________FEED NEW IMAGE mover[i].update(frame) #_________________DRAW rect = mover[i].get_position() pt1 = (int(rect.left()), int(rect.top())) pt2 = (int(rect.right()), int(rect.bottom())) cv2.rectangle(frame, pt1, pt2, (255, 255, 255), 3) full_frame_mover.append((pt1,pt2)) #print(full_frame_mover) # finish 1 frame # ___________________________________________________________________________Call the tracker tracker.predict() tracker.update(detections) # ___________________________________________________________________________DRAW TRACK RECTANGLE for track in tracker.tracks: if track.is_confirmed() and track.time_since_update >1 : continue bbox = track.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,255,255), 2) cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1])+30),0, 5e-3 * 200, (0,255,0),3) dot=int(int(bbox[0])+((int(bbox[2])-int(bbox[0]))/2)),int(bbox[3]-10) cv2.circle(frame,dot, 10, (0,0,255), -1) cv2.imshow('', frame) # Continue until the user presses ESC key if cv2.waitKey(1) == 27: break # END LOOP MOVER # ___________________________________________________________________________Call the tracker tracker.predict() tracker.update(full_frame_mover) # ___________________________________________________________________________DRAW TRACK RECTANGLE for track in tracker.tracks: if track.is_confirmed() and track.time_since_update >1 : continue bbox = track.to_tlbr() cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,255,255), 2) cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1])+30),0, 5e-3 * 200, (0,255,0),3) dot=int(int(bbox[0])+((int(bbox[2])-int(bbox[0]))/2)),int(bbox[3]-10) cv2.circle(frame,dot, 10, (0,0,255), -1) # ___________________________________________________________________________GET POINTS From click if(cv2.waitKey(1)==ord('p')): points = get_lines.run(frame, multi=True) print(points) if points : for line in points: cv2.line(frame, line[0:2], line[2:5], (0,255,255), 2) # draw line cv2.imshow('', frame) print('process time : ',time.time()-tpro) tpro=time.time() if writeVideo_flag: # save a frame out.write(frame) frame_index = frame_index + 1 list_file.write(str(frame_index)+' ') if len(boxs) != 0: for i in range(0,len(boxs)): list_file.write(str(boxs[i][0]) + ' '+str(boxs[i][1]) + ' '+str(boxs[i][2]) + ' '+str(boxs[i][3]) + ' ') list_file.write('\n') if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() if writeVideo_flag: out.release() list_file.close() cv2.destroyAllWindows()
# JUST DRAW LINE for google cloud use from shapely.geometry import Point from shapely.geometry.polygon import Polygon from shapely.ops import cascaded_union import cv2 import get_lines video_capture = cv2.VideoCapture(0) points = [] while True: _, frame = video_capture.read() frame = cv2.resize(frame, (608, 608)) if (cv2.waitKey(1) == ord('p')): points = get_lines.run(frame, multi=True) print(points) if points: for line in points: cv2.line(frame, line[0:2], line[2:4], (0, 255, 255), 2) # draw line cv2.imshow('', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break