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 read(stack): print('Process to read: %s' % os.getpid()) yolo = YOLO() # Definition of the parameters max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 # deep_sort d = os.path.dirname(__file__) model_filename = d + '/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) max_boxs = 0 face = ['17208019'] #目标上一帧的点 history = {} #id和标签的字典 person = {} #赋予新标签的id列表 change = [] while True: if len(stack) != 0: frame = stack.pop() t1 = time.time() frame_count = 0 localtime = time.asctime(time.localtime(time.time())) utils.draw(frame, line.readline()) # 获取警戒线 transboundaryline = line.readline() utils.draw(frame, transboundaryline) img = Image.fromarray(frame) #img.save('frame.jpg') ''' cv2.line(frame, (837, 393), (930, 300), (0, 255, 255), 3) transboundaryline = t.line_detect_possible_demo(frame) ''' # image = Image.fromarray(frame) image = Image.fromarray(frame[..., ::-1]) # bgr to rgb 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] if len(boxs) > max_boxs: max_boxs = len(boxs) # Call the tracker tracker.predict() tracker.update(detections) #一帧信息 info = {} target = [] for track in tracker.tracks: #一帧中的目标 per_info = {} if not track.is_confirmed() or track.time_since_update > 1: continue if track.track_id not in person: person[track.track_id] = str(track.track_id) bbox = track.to_tlbr() PointX = bbox[0] + ((bbox[2] - bbox[0]) / 2) PointY = bbox[3] dis = int(PointX) - 1200 try: if dis < 15: if track.track_id not in change: person[track.track_id] = face.pop(0) change.append(track.track_id) except: print('非法入侵') #当前目标 if track.track_id not in change: per_info['worker_id'] = 'unknow' + str(track.track_id) else: per_info['worker_id'] = person[track.track_id] #当前目标坐标 yoloPoint = (int(PointX), int(PointY)) per_info['current_point'] = yoloPoint # 卡尔曼滤波预测 if per_info['worker_id'] not in utils.KalmanNmae: utils.myKalman(per_info['worker_id']) if per_info['worker_id'] not in utils.lmp: utils.setLMP(per_info['worker_id']) cpx, cpy = utils.predict(yoloPoint[0], yoloPoint[1], per_info['worker_id']) if cpx[0] == 0.0 or cpy[0] == 0.0: cpx[0] = yoloPoint[0] cpy[0] = yoloPoint[1] if frame_count > 20: per_info['next_point'] = (int(cpx), int(cpy)) else: per_info['next_point'] = yoloPoint # 写入越线情况 if per_info['worker_id'] in history: per_info['transboundary'] = 'no' #print(transboundaryline) line1 = [ per_info['next_point'], history[per_info['worker_id']] ] a = line.IsIntersec2(transboundaryline, line1) if a == '有交点': print('越线提醒') per_info['transboundary'] = 'yes' history[per_info['worker_id']] = per_info['current_point'] frame_count = frame_count + 1 #print(per_info) #画目标框 #cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2) cv2.putText(frame, per_info['worker_id'], (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) target.append(per_info) info['time'] = localtime #info['frame'] = str(img.tolist()).encode('base64') info['frame'] = 'frame' info['target'] = target #写入josn info_json = json.dumps(info) info_queue.put(info_json) getInfo(info_queue) cv2.imshow("img", frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break
def Display(): print("Start Displaying") yolo = YOLO() # Definition of the parameters max_cosine_distance = 0.3 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 w = 768 h = 432 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 max_boxs = 0 person_track = {} yolo2 = YOLO2() while True: if q.empty() != True: #读取打卡信息 face = [] cur1 = conn.cursor() # 获取一个游标 sql1 = "select * from worker" cur1.execute(sql1) data = cur1.fetchall() for d in data: # 注意int类型需要使用str函数转义 name = str(d[1]) + '_' + d[2] face.append(name) cur1.close() # 关闭游标 #获取队列帧 frame = q.get() t1 = time.time() #进行安全措施检测 image = Image.fromarray(frame[..., ::-1]) # bgr to rgb img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) frame, wear = yolo2.detect_image(img) frame = np.array(frame) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 获取警戒线 #cv2.line(frame, (132,368), (229, 368), (0, 255, 255), 3) cv2.line(frame, (275,360), (378, 360), (0, 255, 255), 1) transboundaryline = t.line_detect_possible_demo(frame) #yolo目标检测 boxs = yolo.detect_image(image) 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] if len(boxs) > max_boxs: max_boxs = len(boxs) # Call the tracker tracker.predict() tracker.update(detections) for track in tracker.tracks: if max_boxs < track.track_id: tracker.tracks.remove(track) tracker._next_id = max_boxs + 1 if not track.is_confirmed() or track.time_since_update > 1: continue bbox = track.to_tlbr() PointX = bbox[0] + ((bbox[2] - bbox[0]) / 2) PointY = bbox[3] if track.track_id not in person_track: track2 = copy.deepcopy(track) person_track[track.track_id] = track2 else: track2 = copy.deepcopy(track) bbox2 = person_track[track.track_id].to_tlbr() PointX2 = bbox2[0] + ((bbox2[2] - bbox2[0]) / 2) PointY2 = bbox2[3] distance = math.sqrt(pow(PointX - PointX2, 2) + pow(PointY - PointY2, 2)) #修正 if distance < 120: person_track[track.track_id] = track2 else: # print('last',track.track_id) dis = {} for key in person_track: bbox3 = person_track[key].to_tlbr() PointX3 = bbox3[0] + ((bbox3[2] - bbox3[0]) / 2) PointY3 = bbox3[3] d = math.sqrt(pow(PointX3 - PointX, 2) + pow(PointY3 - PointY, 2)) dis[key] = d dis = sorted(dis.items(), key=operator.itemgetter(1), reverse=False) track2.track_id = dis[0][0] person_track[dis[0][0]] = track2 tracker.tracks.remove(track) tracker.tracks.append(person_track[track.track_id]) # 写入class try: box_title = face[track2.track_id - 1] except Exception as e: box_title = str(track2.track_id) + "_" + "unknow" if box_title not in workers: wid = box_title.split('_')[0] localtime = time.asctime(time.localtime(time.time())) workers[box_title] = wk.Worker() workers[box_title].set(box_title, localtime, (int(PointX), int(PointY))) cur2 = conn.cursor() # 获取一个游标 sql2 = "UPDATE worker SET in_time='" + localtime + "' WHERE worker_id= '" + wid + "'" cur2.execute(sql2) cur2.close() # 关闭游标 else: localtime = time.asctime(time.localtime(time.time())) yoloPoint = (int(PointX), int(PointY)) wear_dic = {} workers[box_title].current_point = yoloPoint workers[box_title].track_point.append(workers[box_title].current_point) mytrack = str(workers[box_title].track_point) wid = box_title.split('_')[0] # 卡尔曼滤波预测 if wid not in utils.KalmanNmae: utils.myKalman(wid) if wid not in utils.lmp: utils.setLMP(wid) cpx, cpy = utils.predict(workers[box_title].current_point[0], workers[box_title].current_point[1], wid) if cpx[0] == 0.0 or cpy[0] == 0.0: cpx[0] = workers[box_title].current_point[0] cpy[0] = workers[box_title].current_point[1] workers[box_title].next_point = (int(cpx), int(cpy)) cur3 = conn.cursor() # 获取一个游标 sql3 = "UPDATE worker SET current_point= '" + str(workers[box_title].current_point) + "' ,track_point = '" + mytrack + "',next_point = '" + str(workers[box_title].next_point) + "' WHERE worker_id= '" + wid + "'" cur3.execute(sql3) cur3.close() # 写入安全措施情况 if len(wear) > 0: for w in wear: wear_dis = int(math.sqrt(pow(w[0] - yoloPoint[0], 2) + pow(w[1] - yoloPoint[1], 2))) wear_dic[wear_dis] = w wear_dic = sorted(wear_dic.items(), key=operator.itemgetter(0), reverse=False) if wear_dic[0][0] < 120: cur4 = conn.cursor() # 获取一个游标 if wear[wear_dic[0][1]] == 1: if len(workers[box_title].wear['no helmet']) == 0: workers[box_title].wear['no helmet'].append(localtime) sql = "INSERT INTO wear SET worker_id = '" + wid + "', type = 'no_helmet',abnormal_time = '" + localtime + "'" cur4.execute(sql) cur4.close() # 关闭游标 else: print(box_title,workers[box_title].wear['no helmet']) if localtime not in workers[box_title].wear['no helmet']: workers[box_title].wear['no helmet'].append(localtime) sql = "INSERT INTO wear SET worker_id = '" + wid + "', type = 'no_helmet',abnormal_time = '" + localtime + "'" cur4.execute(sql) cur4.close() # 关闭游标 elif wear[wear_dic[0][1]] == 2: if len(workers[box_title].wear['no work cloths']) == 0: workers[box_title].wear['no work cloths'].append(localtime) sql = "INSERT INTO wear SET worker_id = '" + wid + "', type = 'no work cloths',abnormal_time = '" + localtime + "'" cur4.execute(sql) cur4.close() # 关闭游标 else: if localtime not in workers[box_title].wear['no work cloths']: workers[box_title].wear['no work cloths'].append(localtime) sql = "INSERT INTO wear SET worker_id = '" + wid + "', type = 'no work cloths',abnormal_time = '" + localtime + "'" cur4.execute(sql) cur4.close() # 关闭游标 elif wear[wear_dic[0][1]] == 3: if len(workers[box_title].wear['unsafe wear']) == 0: workers[box_title].wear['unsafe wear'].append(localtime) sql = "INSERT INTO wear SET worker_id = '" + wid + "', type = 'unsafe wear',abnormal_time = '" + localtime + "'" cur4.execute(sql) cur4.close() # 关闭游标 else: if localtime not in workers[box_title].wear['unsafe wear']: workers[box_title].wear['unsafe wear'].append(localtime) sql = "INSERT INTO wear SET worker_id = '" + wid + "', type = 'unsafe wear',abnormal_time = '" + localtime + "'" cur4.execute(sql) cur4.close() # 关闭游标 # 写入越线情况 if len(workers[box_title].track_point) > 4: for i in range(len(transboundaryline)): p1 = (transboundaryline[i][0], transboundaryline[i][1]) p2 = (transboundaryline[i][2], transboundaryline[i][3]) p3 = workers[box_title].track_point[-2] p4 = workers[box_title].track_point[-1] a = t.IsIntersec(p1, p2, p3, p4) if a == '有交点': cur5 = conn.cursor() # 获取一个游标 cur6 = conn.cursor() # 获取一个游标 cur5.execute( "select time from transboundary where worker_id = '" + wid + "' ") qurrytime = cur5.fetchone() cur5.close() # 关闭游标 if qurrytime == None: print('越线') sql = "INSERT INTO transboundary SET worker_id = '" + wid + "',time = '" + localtime + "'" cur6.execute(sql) cur6.close() # 关闭游标 else: temp1 = 0 for qt in qurrytime: if qt == localtime: temp1 = 1 if temp1 == 0: print('越线') sql = "INSERT INTO transboundary SET worker_id = '" + wid + "',time = '" + localtime + "'" cur6.execute(sql) cur6.close() # 关闭游标 if len(workers[box_title].track_point) >= 20: workers[box_title].previous_point = workers[box_title].track_point[-5] conn.commit() try: cv2.putText(frame, face[track2.track_id - 1], (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) except Exception as e: cv2.putText(frame, "unknow", (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) cv2.imshow('', frame) 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') fps = (fps + (1. / (time.time() - t1))) / 2 if cv2.waitKey(1) & 0xFF == ord('q'): break
def main(yolo): # Definition of the parameters max_cosine_distance = 0.3 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 if len(sys.argv) > 1: video_capture = cv2.VideoCapture(sys.argv[1]) else: 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') if len(sys.argv) > 1: file_name = re.split("\.|/", sys.argv[1])[-2] else: file_name = "camera" out = cv2.VideoWriter(file_name + '_output.avi', fourcc, 30, (w, h)) list_file = open(file_name + '_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 t1 = time.time() # image = Image.fromarray(frame) image = Image.fromarray(frame[...,::-1]) #bgr to rgb boxs, scores_ = 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] # Call the tracker tracker.predict() tracker.update(detections) track_str = "" timestamp = time.time() localTime = time.localtime(timestamp) strTime = time.strftime("%Y-%m-%d %H:%M:%S", localTime) track_num = 0 for track in tracker.tracks: if not track.is_confirmed() or 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) # 2019-10-21 if len(scores_) > 0: if track_num >= len(scores_): continue cv2.putText(frame, "id: " + str(track.track_id) + " score: " + str(scores_[track_num])[:6] ,(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2) # 2019/10/21 add track_str if writeVideo_flag: track_str = track_str + str(strTime) + ";" + str(frame_index + 1) + ";" + str(track.track_id) + ";" + str(scores_[track_num])[:6] + ";" + str(boxs[track_num][0]) + ' ' + str(boxs[track_num][1]) + ' ' + str(boxs[track_num][2]) + ' ' + str(boxs[track_num][3]) + ' ' + "\n" track_num += 1 for det in detections: bbox = det.to_tlbr() cv2.rectangle(frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,0,0), 2) cv2.imshow('', frame) if writeVideo_flag: # save a frame out.write(frame) frame_index = frame_index + 1 list_file.write(track_str) # 2019/10/21 """ 2019/10/21 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') """ fps = ( fps + (1./(time.time()-t1)) ) / 2 print("fps= %f"%(fps)) # Press Q to stop! if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() if writeVideo_flag: out.release() list_file.close() cv2.destroyAllWindows()
def main(yolo, url, CreateBoxEncoder, q): producer = None if KAFKA_ON: ip_port = '{}:{}'.format(KAFKA_IP, KAFKA_PORT) producer = KafkaProducer(bootstrap_servers=ip_port) logger.debug('open kafka') # Definition of the parameters max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) door = get_door(url) # init var center_mass = {} miss_ids = [] disappear_box = {} person_list = [] in_house = {} in_out_door = {"out_door_per": 0, "into_door_per": 0} only_id = str(uuid.uuid4()) logger.debug('rtmp: {} load finish'.format(url)) last_person_num = 0 last_monitor_people = 0 while True: t1 = time.time() if q.empty(): continue frame = q.get() image = Image.fromarray(frame[..., ::-1]) # bgr to rgb boxs, scores_ = yolo.detect_image(image) t2 = time.time() # print('5====={}======{}'.format(os.getpid(), round(t2 - t1, 4))) now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) logger.debug("box_num: {}".format(len(boxs))) features = CreateBoxEncoder.encoder(frame, boxs) # score to 1.0 here). # detections = [Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features)] detections = [ Detection(bbox, scores_, feature) for bbox, scores_, feature in zip(boxs, scores_, 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] # Call the tracker tracker.predict() tracker.update(detections) # 实时人员ID保存 track_id_list = [] cv2.rectangle(frame, (door[0], door[1]), (door[2], door[3]), (0, 0, 255), 2) door_half_h = int(int((door[1] + door[3]) / 2) * DOOR_HIGH) cv2.line(frame, (0, door_half_h), (111111, door_half_h), (0, 255, 0), 1, 1) high_score_ids = {} for track in tracker.tracks: # 当跟踪的目标在未来的20帧未出现,则判断丢失,保存至消失的id中间区 if track.time_since_update == MAX_AGE: miss_id = str(track.track_id) miss_ids.append(miss_id) if not track.is_confirmed() or track.time_since_update > 1: continue # 如果人id存在,就把人id的矩形框坐标放进center_mass 否则 创建一个key(人id),value(矩形框坐标)放进center_mass track_id = str(track.track_id) bbox = track.to_tlbr() near_door = is_near_door({track_id: bbox}, door) if track.score >= 0.92 and not near_door: high_score_ids[track_id] = [[ int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]) ]] track_id_list.append(track_id) if track_id in center_mass: center_ = center_mass.get(track_id) if len(center_) > 49: center_.pop(0) center_.append( [int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])]) else: center_mass[track_id] = [[ int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]) ]] # # -------------------------------------------- # # logger.debug('box1:{}'.format([int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])])) cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) x0, y0 = int((bbox[0] + bbox[2]) / 2), int((bbox[1] + bbox[3]) / 2) cv2.putText(frame, str(round(track.score, 3)), (x0, y0), 0, 0.6, (0, 255, 0), 2) # cv2.circle(frame, (x0, y0), 2, (0, 255, 255), thickness=2, lineType=1, shift=0) # # -------------------------------------------- # x0, y0 = int((bbox[0] + bbox[2]) / 2), int((bbox[1] + bbox[3]) / 2) # w = abs(int(bbox[3]) - int(bbox[1])) # h = abs(int(bbox[2]) - int(bbox[0])) logger.info('id:{}, score:{}'.format(track_id, track.score)) for id in miss_ids: if id in center_mass.keys(): disappear_box[id] = center_mass[id] del center_mass[id] miss_ids.clear() # # 进出门判断 out_or_in(center_mass, door, in_house, disappear_box, in_out_door) # near_door = is_near_door(center_mass, door, disappear_id) # 相对精准识别人 用来实时传递当前人数 box_score_person = [scores for scores in scores_ if scores > 0.72] person_sum = in_out_door['into_door_per'] - in_out_door['out_door_per'] # if person_sum <= len(high_score_ids) and not near_door: if person_sum <= len(high_score_ids): # 当时精准人数大于进出门之差时 来纠正进门人数 并把出门人数置为0 if person_sum == len(high_score_ids) == 1: pass # print('person_sum == len(high_score_ids) == 1') else: logger.warning('reset in_out_door person') in_out_door['out_door_per'] = 0 in_out_door['into_door_per'] = len(high_score_ids) in_house.update(high_score_ids) # print('high score:{}'.format(high_score_ids)) logger.warning('22222222-id: {} after into of door: {}'.format( in_house.keys(), in_out_door['into_door_per'])) person_sum = len(high_score_ids) if in_out_door['into_door_per'] == in_out_door['out_door_per'] > 0: in_out_door['into_door_per'] = in_out_door['out_door_per'] = 0 if len(person_list) > 100: person_list.pop(0) person_list.append(person_sum) # 从url提取摄像头编号 pattern = str(url)[7:].split(r"/") logger.debug('pattern {}'.format(pattern[VIDEO_CONDE])) video_id = pattern[VIDEO_CONDE] logger.info('object tracking cost {}'.format(time.time() - t1)) # 当列表中都是0的时候 重置进出门人数和所有字典参数变量 if person_list.count(0) == len(person_list) == 101: logger.debug('long time person is 0') in_out_door['into_door_per'] = 0 in_out_door['out_door_per'] = 0 in_house.clear() logger.warning('All Clear') cv2.putText(frame, "person: " + str(person_sum), (40, 40), 0, 5e-3 * 200, (0, 255, 0), 2) cv2.putText(frame, "now_per: " + str(len(box_score_person)), (280, 40), 0, 5e-3 * 200, (0, 255, 0), 2) # 当满足条件时候 往前端模块发送人员的信息 if (last_person_num != person_sum or last_monitor_people != len(box_score_person)) and producer: monitor_people_num = len(box_score_person) logger.debug("person-sum:{} monitor-people_num:{}".format( person_sum, monitor_people_num)) # if int(time.time()) - last_time >= 1: cv2.imwrite( "/opt/code/deep_sort_yolov3/image/{}.jpg".format( str(uuid.uuid4())), frame) # print('save img success') save_to_kafka(TOPIC_SHOW, now, person_sum, url, producer, video_id, monitor_people_num, only_id) if last_person_num > 0 and person_sum == 0: only_id = str(uuid.uuid4()) if last_person_num == 0 and person_sum > 0: save_to_kafka(TOPIC_NVR, now, person_sum, url, producer, video_id, len(box_score_person), only_id) # last_time = int(time.time()) last_person_num = person_sum last_monitor_people = len(box_score_person) # 当满足条件时候 往NVR模块发送信息 logger.info('url:{} into_door_per: {}'.format( url, in_out_door['into_door_per'])) logger.info('url:{} out_door_per: {}'.format( url, in_out_door['out_door_per'])) logger.info('url:{} in_house: {}'.format(url, in_house)) logger.info('url:{} monitor_people_num: {}'.format( url, len(box_score_person))) logger.info('url:{} person_sum: {}'.format(url, person_sum)) logger.info('GPU image load cost {}'.format(time.time() - t1)) t3 = time.time() fps = round(1 / (round(t3 - t1, 4)), 3) # print('pid:{}===fps:{}===time:{}'.format(os.getpid(), fps, round(t3 - t1, 4))) # print('*' * 30) fps = ((1 / (time.time() - t1))) logger.debug("fps= %f" % (fps)) cv2.imshow('', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
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 read(stack): print('Process to read: %s' % os.getpid()) yolo = YOLO() # Definition of the parameters max_cosine_distance = 0.3 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) max_boxs = 0 face = ['A'] # face = [] # cur1 = conn.cursor() # 获取一个游标 # sql = "select * from worker" # cur1.execute(sql) # data = cur1.fetchall() # for d in data: # # 注意int类型需要使用str函数转义 # name = str(d[1]) # # face.append(name) # cur1.close() # 关闭游标 yolo2 = YOLO2() #目标上一帧的点 history = {} #id和标签的字典 person = {} #赋予新标签的id列表 change = [] while True: if len(stack) != 0: frame = stack.pop() t1 = time.time() localtime = time.asctime(time.localtime(time.time())) # 进行安全措施检测 #frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) img = Image.fromarray(frame) #img.save('frame.jpg') frame, wear = yolo2.detect_image(img) frame = np.array(frame) # frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # 获取警戒线 transboundaryline = line.readline() utils.draw(frame, transboundaryline) # image = Image.fromarray(frame) image = Image.fromarray(frame[..., ::-1]) # bgr to rgb 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] if len(boxs) > max_boxs: max_boxs = len(boxs) # Call the tracker tracker.predict() tracker.update(detections) #一帧信息 info = {} target = [] for track in tracker.tracks: #一帧中的目标 per_info = {} if not track.is_confirmed() or track.time_since_update > 1: continue if track.track_id not in person: person[track.track_id] = str(track.track_id) bbox = track.to_tlbr() PointX = bbox[0] + ((bbox[2] - bbox[0]) / 2) PointY = bbox[3] dis = int(PointX) - 1200 try: if dis < 15: if track.track_id not in change: person[track.track_id] = face.pop(0) change.append(track.track_id) except: print('非法入侵') #当前目标 if track.track_id not in change: per_info['worker_id'] = 'unknow' + str(track.track_id) else: per_info['worker_id'] = person[track.track_id] #当前目标坐标 yoloPoint = (int(PointX), int(PointY)) per_info['current_point'] = yoloPoint # 卡尔曼滤波预测 if per_info['worker_id'] not in utils.KalmanNmae: utils.myKalman(per_info['worker_id']) if per_info['worker_id'] not in utils.lmp: utils.setLMP(per_info['worker_id']) cpx, cpy = utils.predict(yoloPoint[0], yoloPoint[1], per_info['worker_id']) if cpx[0] == 0.0 or cpy[0] == 0.0: cpx[0] = yoloPoint[0] cpy[0] = yoloPoint[1] per_info['next_point'] = (int(cpx), int(cpy)) # 写入安全措施情况 wear_dic = {} per_info['wear'] = 'safe wear' if len(wear) > 0: for w in wear: wear_dis = int( math.sqrt( pow(w[0] - yoloPoint[0], 2) + pow(w[1] - yoloPoint[1], 2))) wear_dic[wear_dis] = w wear_dic = sorted(wear_dic.items(), key=operator.itemgetter(0), reverse=False) if wear_dic[0][0] < 180: if wear[wear_dic[0][1]] == 1: per_info['wear'] = 'no_helmet' elif wear[wear_dic[0][1]] == 2: per_info['wear'] = 'no work cloths' elif wear[wear_dic[0][1]] == 3: per_info['wear'] = 'unsafe wear' # 写入越线情况 if per_info['worker_id'] in history: per_info['transboundary'] = 'no' # print(transboundaryline) line1 = [ per_info['next_point'], history[per_info['worker_id']] ] a = line.IsIntersec2(transboundaryline, line1) if a == '有交点': print('越线提醒') per_info['transboundary'] = 'yes' history[per_info['worker_id']] = per_info['current_point'] #print(per_info) #画目标框 #cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2) cv2.putText(frame, per_info['worker_id'], (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) target.append(per_info) info['time'] = localtime #info['frame'] = str(img.tolist()).encode('base64') info['frame'] = 'frame' info['target'] = target #写入josn info_json = json.dumps(info) info_queue.put(info_json) getInfo(info_queue) cv2.imshow("img", frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break
from pygame import mixer from datetime import datetime from timeit import time def tone_loop(tone,stop_key): mixer.init() mixer.music.load(tone) mixer.music.set_volume(2) mixer.music.play() while True: user_input=input("") if user_input==stop_key: mixer.music.stop() break def log(msg): with open("mylogs.txt","a") as f: f.write(f"{msg},{datetime.now()},\n") if __name__ == '__main__': routine = time.localtime() while True : if routine.tm_hour==14 and routine.tm_min==32: print("Water Drinking time. Enter 'drank' to stop the alarm.") tone_loop('C:\\Users\\jiten\\Downloads\\Music\\music (4).mp3', 'drank') init_water = time() log("Drank Water at") if routine.tm_hour == 14 and routine.tm_min == 16: print("Water Drinking time. Enter 'drank' to stop the alarm.") tone_loop('C:\\Users\\jiten\\Downloads\\Music\\music (4).mp3', 'drank') init_water = time() log("Drank Water at") routine.tm_min+1