def predict(): data = {"success": False} if flask.request.method == "POST": if flask.request.files.get("image"): image = flask.request.files["image"].read() image = Image.open(io.BytesIO(image)) image = prepare_image(image) image = image.copy(order="C") k = str(uuid.uuid4()) d = {"id": k, "image": base64_encode_image(image), "h": image.shape[0], "w": image.shape[1] } db.rpush(IMAGE_QUEUE, json.dumps(d)) while True: output = db.get(k) if output is not None: output = output.decode("utf-8") data["predictions"] = json.loads(output) db.delete(k) break time.sleep(CLIENT_SLEEP) data["success"] = True return flask.jsonify(data)
def debug_msg(self, msg, warn=False): """ Output a colored message or warning, incrementing the step_counter to enable a pdb trace to be set at any point a verbose message is printed. """ color = 2 if warn: color = 1 if self.verbose: text = 'Step: %s // --> %s\n' % (self.step_counter, msg) sys.stderr.write(color_text(color, text, self.no_color)) if self.pause: for second in range(1, (int(self.pause) + 1)): sys.stderr.write('%s ' % color_text(5, second, self.no_color)) time.sleep(1) sys.stderr.flush() sys.stderr.write('... \n') if self.trace_steps: if self.step_counter in self.trace_steps: try: print ">>> Entering PDB interpreter (press 'c' to leave)" set_trace() except KeyboardInterrupt: pass self.step_counter += 1
def debug_msg(self, msg, warn=False): """ Output a colored message or warning, incrementing the step_counter to enable a pdb trace to be set at any point a verbose message is printed. """ color = 2 if warn: color = 1 if self.verbose: text = "Step: %s // --> %s\n" % (self.step_counter, msg) sys.stderr.write(color_text(color, text, self.no_color)) if self.pause: for second in range(1, (int(self.pause) + 1)): sys.stderr.write("%s " % color_text(5, second, self.no_color)) time.sleep(1) sys.stderr.flush() sys.stderr.write("... \n") if self.trace_steps: if self.step_counter in self.trace_steps: try: print ">>> Entering PDB interpreter (press 'c' to leave)" set_trace() except KeyboardInterrupt: pass self.step_counter += 1
def initial_requirements_width(frame): ## This is a function where the user will select two pixel locations in the frame that are 1 FEET away in the real world image. print("CLICK and SELECT TWO POINTS WHICH ARE 1 FEET APART in the FRAME") print('PRESS "ENTER" TO EXIT After clicking') refPt = [] def click_event(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: refPt.append([x, y]) cv2.circle(clone, (x, y), 5, (255, 255, 255), -1) cv2.imshow("clone", clone) if event == cv2.EVENT_RBUTTONDOWN: blue = clone[y, x, 0] green = clone[y, x, 1] red = clone[y, x, 2] font = cv2.FONT_HERSHEY_SIMPLEX strBGR = str(blue) + ", " + str(green) + "," + str(red) cv2.putText(clone, strBGR, (x, y), font, 0.1, (255, 255, 255), 2) cv2.imshow("clone", clone) clone = frame.copy() cv2.imshow("clone", clone) cv2.setMouseCallback("clone", click_event) cv2.waitKey(0) cv2.destroyAllWindows() refPt = np.asarray(refPt) if refPt.shape[0] < 2: raise ValueError("SELECT AT-LEAST TWO POINTS !!!!") xA = refPt[-1, 0] yA = refPt[-1, 1] xB = refPt[-2, 0] yB = refPt[-2, 1] reference_width = dist.euclidean((xA, yA), (xB, yB)) time.sleep(2) return reference_width
def clean_after_work(): files = glob.glob(f'{BASE_PATH}/merge/*.pdf') for f in files: try: time.sleep(0.02) os.remove(f) except OSError as e: print("Ошибка: %s : %s" % (f, e.strerror)) print('Очистка папки выполнена!')
def get_frame(): w = 640 h = 480 time.sleep(0.06) while True: jpeg = dt["img"] #print(len(jpeg)) if len(jpeg) == 0: continue return jpeg
def read_frame(self): print("开启推流") # Get video information #fps = int(cap.get(cv.CAP_PROP_FPS)) #width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH)) #height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT)) sizeStr = "{}x{}".format(640, 360) rtmp_fps = 20 # ffmpeg command self.command = ['ffmpeg', '-y', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-pix_fmt', 'bgr24', '-s', sizeStr, '-r', str(rtmp_fps), '-i', '-', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-preset', 'ultrafast', '-f', 'flv', self.rtmpUrl] # read webcamera video_capture = cv2.VideoCapture(self.camera_path) if video_capture.isOpened() != True: video_capture.open(self.camera_path) while True: ret, frame = video_capture.read() # if ret != True: # print(':---------------------------reset---------------------------------------') # video_capture.release() # video_capture = cv2.VideoCapture(self.camera_path) # if video_capture.isOpened() != True: # video_capture.open(self.camera_path) # # continue if ret: #frame = np.rot90(frame, -1) frame = cv2.resize(frame, dsize=(VIDEO_HEIGHT, VIDEO_WEIGHT), interpolation=cv2.INTER_CUBIC) # cv2.imshow('frame', frame) # put frame into queue print('Receive inqueue') self.frame_queue.put(frame) time.sleep(0.001) else: print('read empty') video_capture.release() video_capture = cv2.VideoCapture(self.camera_path) if video_capture.isOpened() != True: video_capture.open(self.camera_path)
def post_event(self): while True: print('self.post_queue.empty:', self.post_queue.empty()) if self.post_queue.empty()!= True: (frame,post_data) = self.post_queue.get() print('post event') # url_request = "http://www.kangni.com/resource/addEvent" # files = {'file': ('pic.jpg', io.BytesIO(cv2.imencode('.jpg', frame)[1].tostring()), 'image/jpeg')} # r = requests.post(url_request, files=files, data=post_data) # print(r.text) time.sleep(0.1)
def initial_requirements_temperature(frame, selected_reference_temperature): ## This is a function where the user will select a pixel location in the frame which the desired reference temperature i.e "selected_reference_temperature". ## Needed to calculate the Facial Temperature of the detected person bounding boxes. print( "CLICK and SELECT THE POINT FOR THE REFERENCE TEMPERATURE=%s C ----- " "IF YOU CLICK on MULTIPLE POINTS, the LAST POINT WILL BE SELECT" % selected_reference_temperature) print('PRESS "ENTER" TO EXIT After clicking') refPt = [] def click_event(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: refPt.append([x, y]) font = cv2.FONT_HERSHEY_SIMPLEX strXY = str(x) + ", " + str(y) cv2.putText(clone, strXY, (x, y), font, 0.5, (255, 255, 0), 2) cv2.imshow("clone", clone) if event == cv2.EVENT_RBUTTONDOWN: blue = clone[y, x, 0] green = clone[y, x, 1] red = clone[y, x, 2] font = cv2.FONT_HERSHEY_SIMPLEX strBGR = str(blue) + ", " + str(green) + "," + str(red) cv2.putText(clone, strBGR, (x, y), font, 0.5, (0, 255, 255), 2) cv2.imshow("clone", clone) clone = frame.copy() cv2.imshow("clone", clone) cv2.setMouseCallback("clone", click_event) cv2.waitKey(0) cv2.destroyAllWindows() refPt = np.asarray(refPt) reference_px = int(refPt[-1, 0]) reference_py = int(refPt[-1, 1]) print("REFERENCE POINT HAS X=%d and Y=%d" % (reference_px, reference_py)) time.sleep(2) return reference_px, reference_py
def detect_process(): model = YOLO() while True: queue = db.lrange(IMAGE_QUEUE, 0, BATCH_SIZE - 1) imageIDs = [] batch = None for q in queue: q = json.loads(q.decode("utf-8")) image = base64_decode_image(q["image"], IMAGE_DTYPE, (BATCH_SIZE, q["h"], q["w"], IMAGE_CHANS)) if batch is None: batch = image else: batch = np.vstack([batch, image]) imageIDs.append(q["id"]) if len(imageIDs) > 0: print("* Batch size: {}".format(batch.shape)) for imageID, image in zip(imageIDs, batch): image = Image.fromarray(image.astype("uint8")) boxs = model.detect_image(image) output = [] for b in boxs: r = {"xywh": [b[:4]], "score": 1*b[4], "class": 1*b[5]+0.0} output.append(r) db.set(imageID, json.dumps(output)) db.ltrim(IMAGE_QUEUE, len(imageIDs), -1) time.sleep(SERVER_SLEEP)
def main(): # Definition of the parameters max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 tsocket = TSocket.TSocket(__HOST, __PORT) transport = TTransport.TFramedTransport(tsocket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Client(protocol) extract_rate = 5 #抽帧频率 transport.open() # deep_sort metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) video_capture = cv2.VideoCapture("MOT16-09.mp4") frame_rate = video_capture.get(5) sample_interval = 1. / extract_rate print(frame_rate, extract_rate, sample_interval) delay = 1. / frame_rate print(delay) fps = 0.0 ############################################## loc_dic = {} in_count = 0 #in 计数器 out_count = 0 #out 计数器 ############################################## frame_count = 0 global last_stat_time last_stat_time = time.time() w = 640 h = 480 last_sample_time = 0.0 while True: start = time.time() ret, frame = video_capture.read() if ret != True: break frame = cv2.resize(frame, (w, h)) now = time.time() if last_sample_time + sample_interval <= now: t1 = time.time() boxes, features = encode( client, frame) #image压缩为jpg格式,发送到gpu server进行yolov3检测,得到features后返回 last_sample_time = time.time() nfps = 1. / (time.time() - t1) print(nfps) if fps <= 0.1: fps = nfps else: fps = (fps + nfps) / 2 print("detection fps= %f" % (fps)) #print(features[0])#128 tt1 = time.time() detections = [ Detection(bbox, 1.0, feature) for bbox, feature in zip(boxes, features) ] #print(detections) # 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) print("tracker used:", time.time() - tt1) for track in tracker.tracks: #print(track.track_id) 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) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) id_num = str(track.track_id) if id_num in loc_dic: #判断上一帧运动轨迹 #向右运动,且经过分界线 last_x = loc_dic[id_num] if bbox[0] > last_x and (bbox[0] > float(w / 2) and last_x < float(w / 2)): print("##################in one#################") loc_dic[id_num] = bbox[0] in_count += 1 #向左移动,且经过分界线 elif bbox[0] < last_x and (bbox[0] < float(w / 2) and last_x > float(w / 2)): print("###################out one################") loc_dic[id_num] = bbox[0] out_count += 1 else: loc_dic[id_num] = bbox[0] 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) frame_count += 1 cv2.line(frame, (int(w / 2), int(0)), (int(w / 2), int(h)), (255, 255, 255)) cv2.putText(frame, "in number:" + str(in_count), (10, 40), 0, 1e-3 * h, (255, 0, 0), 2) cv2.putText(frame, "out number:" + str(out_count), (10, 60), 0, 1e-3 * h, (255, 0, 0), 2) ret, frame = cv2.imencode('.jpg', frame) dt["img"] = frame.tobytes() wait_time = delay - (time.time() - start) #print(wait_time) if wait_time > 0: time.sleep(wait_time) video_capture.release()
def process_frame(): # 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 = False #video_capture = cv2.VideoCapture(0) producer = KafkaProducer( bootstrap_servers='master:6667', value_serializer=lambda m: json.dumps(m).encode('utf8')) consumer = KafkaConsumer('test', bootstrap_servers=['master:6667']) for msg in consumer: json_from_consumer = json.loads(msg[-6]) decoded = base64.b64decode(json_from_consumer['image']) filename = '/home/haohsiang/Vigilancia-Distributed/codev1frame.jpg' # I assume you have a way of picking unique filenames with open(filename, 'wb') as f: f.write(decoded) frame = cv2.imread(filename) #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 = 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) 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) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) print( str(track.track_id) + ' :' + str(bbox[0]) + ' ' + str(bbox[1]) + ' ' + str(bbox[2]) + ' ' + str(bbox[3])) print(dt.datetime.now().time()) result = { 'ID': str(track.track_id), 'timestamp': dt.datetime.now().isoformat(), 'location_x': str(bbox[0]), 'w': str(bbox[2]) } producer.send('resultstream', result) time.sleep(0.3) 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(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 = 0.0 fps = (fps + (1. / (time.time() - t1))) / 2 print("fps= %f" % (fps)) # Press Q to stop! if cv2.waitKey(1) & 0xFF == ord('q'): break
def extract_query(initial_id,r_id,cam_id,flag,unique_id): ''' Extract features and perform cosine similarity to dertimine re-identification Objective: 1. Read images extracted from YOLO+DeepSORT and store features, afterwhich image will be deleted 2. Compare features of all images 3. If feature match > 70%, store both IDs to be replaced in main tracker 4. Store all initial re-identifications and updated ones onto text file 5. Clear extracted features and repeat process ''' reid_full = open('logs/reid_full.txt','w') start = time.time() while not flag.is_set(): time.sleep(5) #reset all stored info when called again (prevents continous stacking) (Objective 5) query_features.clear() q_id.clear() gallery_features.clear() g_id.clear() cam_num.clear() query_list = sorted(os.listdir('./images/query')) gallery_list = sorted(os.listdir('./images/gallery')) #Read and Extract features from images (Objective 1) for file in query_list: image = cv2.imread(os.path.join('./images/query',file)) result = lomo.LOMO(image,lomo_config) query_features.append(result)#Append to list q_id.append(file.split('.')[0]) os.remove('./images/query/'+file)#Remove image after features extracted for file in gallery_list: image = cv2.imread(os.path.join('./images/gallery',file)) camera_id = file.split('_')[1].split('.')[0] result = lomo.LOMO(image,lomo_config) gallery_features.append(result) g_id.append(file.split('_')[0]) cam_num.append(camera_id)#Append camera number os.remove('./images/gallery/'+file) # Comparsion of features (Objective 2) for cam in range(1,len(source_names)): for i in range(len(query_features)): highest_score = 0 for j in range(len(gallery_features)): if not int(cam_num[j]) == cam+1: continue cos_sim = 1 - spatial.distance.cosine(query_features[i],gallery_features[j]) if cos_sim > 0.7: if cos_sim > highest_score: highest_score = cos_sim query_num = i gallery_num = j source_names # Store matched ID in inital_id for replacing (Objective 3) if not highest_score == 0: #Update if int(g_id[gallery_num]) in initial_id: #If initial camID is already in list index = initial_id.index(int(g_id[gallery_num])) #Get index of camID stored if not r_id[index] == int(q_id[query_num]): #Ensure that the 2 targets are not previously defined as a match r_id[index] = int(q_id[query_num]) #Update value print('ID '+g_id[gallery_num]+' updated to '+q_id[query_num]) # (Objective 4) reid_full.write('ID '+g_id[gallery_num]+' updated to '+q_id[query_num]+' at '+str(round(time.time()-start))+' seconds') reid_full.write('\n') else: pass #New creation elif int(q_id[query_num]) not in r_id and int(g_id[gallery_num]) not in initial_id and not (g_id[gallery_num] == q_id[query_num]): initial_id.append(int(g_id[gallery_num])) #Create and append CAM 1 id into r_id which r_id is a global list in the Manager #r_id contains matches with CAM 1 r_id.append(int(q_id[query_num])) unique_id.append(unique_prefix+str(len(initial_id))) print(q_id[query_num] +' identified with '+g_id[gallery_num]+' on camera '+cam_num[gallery_num]) # (Objective 4) reid_full.write(q_id[query_num] +' identified with '+g_id[gallery_num]+' on camera '+cam_num[gallery_num]+' at '+str(round(time.time()-start))+' seconds') reid_full.write('\n') reid_full.close()
def slaveloop(yoloall): """从机的循环""" # init web cam fps = 0 writer_fps = 25 # 保存帧率 writeVideo_flag = False # Save video flag video_capture = cv2.VideoCapture(0) # video_capture = cv2.VideoCapture(1) # usb cam video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) w = int(video_capture.get(3)) h = int(video_capture.get(4)) print('Camera resolution:', (w, h)) # 存储 if writeVideo_flag: fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('./output/output.avi', fourcc, writer_fps, (w, h)) """main loop""" while True: # stream ret, frame = video_capture.read() frame = cv2.flip(frame, 180) # 图像翻转,确定与真实方向一致 if ret is not True: break t1 = time.time() # for fps put text image = Image.fromarray(frame[..., ::-1]) # bgr to rgb 仅yolo使用 """detector res""" boxs, confidence, class_names = yoloall.detect_image(image) """translator处理预测结果""" boxs_xyxy = translate.xywh2xyxy(boxs) # 转换坐标 translator.arrange_person(boxs_xyxy, confidence, class_names) # person排列和过滤 translator.assign_object2person(boxs_xyxy, confidence, class_names) # hand和eye关联和过滤 translator.voting() # 稳定输出结果 # print('translator.id_hand_status: ', translator.id_hand_status) # print('translator.id_eye_status: ', translator.id_eye_status) """ socket 接收和发送结果 从机: 发送:translator 结果 {id:{hand,hand.eye}} 接受:cfg -> GUI [0]显示结果,[1]游戏逻辑变量 [2]processor结果 """ lan_slave.send(translator) receive = lan_slave.receive() """processor集成所有结果,从机不需要""" """游戏流程""" if receive['stage'] == 0: # 0.赋予角色流程 if receive['flag_jump'] is True: # 跳转页面 gui.stackedWidget.setCurrentIndex(4) app.processEvents() if receive['gui_clean'] is True: # 清屏 gui.clean() time.sleep(3) gui.assign(str(receive['assign_id']), receive['assign_ch'], receive['countdown_pro']) app.processEvents() elif receive['stage'] == 1: # 1.晚上:狼人行动 if receive['flag_jump'] is True: # 跳转页面 gui.jump_werewolf() app.processEvents() gui.update_werewolf(receive['werewolf_voting'], receive['confirm_pro'], receive['countdown_pro'], receive['flag_confirm']) app.processEvents() elif receive['stage'] == 2: # 2.晚上:预言家行动 if receive['flag_jump'] is True: # 跳转页面 gui.jump_seer() app.processEvents() gui.update_seer(receive['seer_voting'], receive['seer_res'], receive['confirm_pro'], receive['countdown_pro'], receive['flag_confirm']) # 直到预言家确认才能给出验人状态 app.processEvents() elif receive['stage'] == 3: # 3.晚上:女巫救人 if receive['flag_jump'] is True: # 跳转页面 time.sleep(3) gui.jump_witch_save() app.processEvents() gui.witch_save(receive['kill_id'], receive['witch_thumb'], receive['confirm_pro'], receive['countdown_pro'], receive['flag_confirm']) app.processEvents() elif receive['stage'] == 4: # 4.晚上:女巫毒人 if receive['flag_jump'] is True: # 跳转页面 gui.jump_witch_poison() app.processEvents() gui.witch_poison(receive['witch_digit'], receive['confirm_pro'], receive['countdown_pro'], receive['flag_confirm']) app.processEvents() elif receive['stage'] == 5: # 5.天亮,宣布死讯 if receive['flag_jump'] is True: # 跳转页面 gui.jump_announce_night() app.processEvents() gui.announce_night(receive['day'], receive['annouce_killed'], receive['annouce_poisoned']) app.processEvents() elif receive['stage'] == 6: # 6.天亮:讨论 if receive['flag_jump'] is True: # 跳转页面 gui.jump_status() app.processEvents() gui.status(receive['status_list']) # 更新玩家状态表 app.processEvents() elif receive['stage'] == 7: # 7.天亮:投票 if receive['flag_jump'] is True: # 跳转页面 gui.jump_vote() app.processEvents() gui.vote(receive['voting_day'], receive['confirm_pro'], receive['countdown_pro']) app.processEvents() elif receive['stage'] == 8: # 8 特殊: 猎人开枪 if receive['flag_jump'] is True: # 跳转页面 gui.jump_hunter() app.processEvents() gui.hunter(receive['voting_hunter'], receive['confirm_pro'], receive['countdown_pro']) app.processEvents() elif receive['stage'] == 9: # 9. 结束:游戏结束 if receive['flag_jump'] is True: # 跳转页面 if receive['gameover'] == 1: # 好人获胜 gui.jump_gameover(1) elif receive['gameover'] == 2: # 坏人获胜 gui.jump_gameover(2) else: print('error: 一个不存在的人获得了胜利') app.processEvents() time.sleep(5) elif receive['stage'] == -1: # 游戏结束:跳出程序 print('alive list: ', receive['end_game_list']) print('this is the game progress{day: [killed_id, poisoned_id, flag_save,]:', receive['end_game_progress']) print('Thank you for playing our game!') break else: pass """GUI 刷新""" app.processEvents() # GUI刷新 # test visual img new_frame = yoloall.vis(frame, boxs, confidence, class_names) # 存储 if writeVideo_flag: out.write(frame) """测试用:预测结果可视化输出""" fps = (fps + (1. / (time.time() - t1))) / 2 cv2.putText(new_frame, "FPS: %f" % (fps), (int(20), int(40)), 0, 5e-3 * 200, (145, 145, 145), 2) cv2.namedWindow("YOLO4_Deep_SORT", 0) # cv2.resizeWindow('YOLO4_Deep_SORT', 1024, 768) cv2.resizeWindow('YOLO4_Deep_SORT', w, h) cv2.imshow('YOLO4_Deep_SORT', new_frame) # Press Q to stop! if cv2.waitKey(1) & 0xFF == ord('q'): break # exit system if writeVideo_flag: out.release() cv2.destroyAllWindows() video_capture.release()
def hostloop(yoloall): # init web cam fps = 0 writer_fps = 25 # 保存帧率 writeVideo_flag = False # Save video flag video_capture = cv2.VideoCapture(0) # video_capture = cv2.VideoCapture(1) # usb cam video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) w = int(video_capture.get(3)) h = int(video_capture.get(4)) print('Camera resolution:', (w, h)) # 存储 if writeVideo_flag: fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('./output/output.avi', fourcc, writer_fps, (w, h)) """main loop""" while True: # stream ret, frame = video_capture.read() frame = cv2.flip(frame, 180) # 图像翻转,确定与真实方向一致 if ret is not True: break t1 = time.time() # for fps put text image = Image.fromarray(frame[..., ::-1]) # bgr to rgb 仅yolo使用 """detector res""" boxs, confidence, class_names = yoloall.detect_image(image) """translator处理预测结果""" boxs_xyxy = translate.xywh2xyxy(boxs) # 转换坐标 translator.arrange_person(boxs_xyxy, confidence, class_names) # person排列和过滤 translator.assign_object2person(boxs_xyxy, confidence, class_names) # hand和eye关联和过滤 translator.voting() # 稳定输出结果 """ 单设备不需要 socket 接收和发送结果 主机: 发送:cfg -> GUI 显示结果, 游戏流程 接受:translator 结果 {id:{hand,hand.eye}} """ if cfg.num_devices == 1: pass else: translator_slave = lan_host.receive() # 接受从机translator结果 """processor集成所有结果""" if cfg.num_devices == 1: # 单机结果 processor.get_hand(translator.id_hand_status, 0) # 将双手结果相加,thumbs只取左手或一只手的结果, 不拼接 processor.concat_eyes(translator.id_eye_status, 0) # 将眼睛结果传递, 不拼接 else: # 拼接 processor.get_hand(translator.id_hand_status, translator_slave.id_hand_status) # 将双手结果相加,thumbs只取左手或一只手的结果, 拼接从机结果 processor.concat_eyes(translator.id_eye_status, translator_slave.id_eye_status) # 拼接从机结果 # print(translator.id_hand_status) # print(translator_slave.id_hand_status) # print(processor.get_hand(translator.id_hand_status,translator_slave.id_hand_status)) """lan2lan 接收和发送结果(发送translator结果),单设备不需要""" if cfg.num_devices == 1: # 单机结果 pass else: # 发送给从机 send_data = {'stage': cfg.stage, 'flag_jump': cfg.flag_jump, 'assign_id': cfg.assign_id, 'assign_ch': type( game.players[cfg.assign_id]).__name__ if cfg.assign_id <= cfg.num_players else "", # assign ch不能大于人数 'countdown_pro': cfg.countdown_pro, 'gui_clean': cfg.flag_audio_start, 'werewolf_voting': str(processor.get_voting_digit(game.spec_id['Werewolf'])), 'confirm_pro': cfg.confirm_pro, 'flag_confirm': cfg.flag_confirm, 'seer_voting': str(processor.get_voting_digit(game.spec_id['Seer'])), 'seer_res': game.movement_seer(processor.get_voting_digit(game.spec_id['Seer'])) if ( processor.get_voting_digit( game.spec_id[ 'Seer']) <= cfg.num_players and cfg.confirm_pro >= cfg.comfirm_thres) or ( processor.get_voting_digit( game.spec_id[ 'Seer']) <= cfg.num_players and cfg.countdown_pro <= 0) else 2, 'kill_id': game.progress[cfg.day][0], 'witch_thumb': processor.get_thumbs(game.spec_id['Witch']), 'witch_digit': str(processor.get_voting_digit(game.spec_id['Witch'])), 'day': cfg.day, 'annouce_killed': game.announce_night(cfg.day)[0], 'annouce_poisoned': game.announce_night(cfg.day)[1], 'status_list': game.get_status_list(), 'voting_day': processor.voting_day(game.get_alive_idlist()), 'voting_hunter': str(processor.get_voting_digit(game.spec_id['Hunter'])), 'gameover': game.is_gameover(game.get_character_list()), 'end_game_list': game.get_status_list(), 'end_game_progress': game.progress, } lan_host.send(send_data) """游戏流程""" if cfg.stage == 0: # 0.赋予角色流程 if cfg.flag_audio_background is True: # 播放背景音频 au_background.play_aud_dirc('bgm') cfg.flag_audio_background = False if cfg.flag_audio_start is True: time.sleep(3) # 两个玩家之间等待一点时间 au.play_aud_merge(str(cfg.assign_id), 'assign_open') # 11:睁眼, 玩家id-1 cfg.flag_audio_start = False processor.canopen_list = [cfg.assign_id] # 被赋予角色的玩家可以睁眼 if processor.check_cheat() == 0: # 无人作弊 gui.assign(str(cfg.assign_id), type(game.players[cfg.assign_id]).__name__, t=cfg.countdown_pro) app.processEvents() # 倒计时 if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate elif cfg.countdown_pro <= 0: # 倒计时结束, 下一个玩家 cfg.countdown_pro = cfg.countdown_thres # 重置倒计时 gui.clean() # 清空页面 au.play_aud_merge(str(cfg.assign_id), 'assign_close') # 播放语音 cfg.assign_id += 1 # 更新id cfg.flag_audio_start = True # 允许播放开始语音 if cfg.assign_id > cfg.num_players: # 角色赋予流程结束,进行下一个步骤,更新页面跳转flag, 更新audio time.sleep(5) # 流程 reset_flag() elif processor.check_cheat() != 0: # 有人睁眼 # 加:提示页面和提示语音,如何加标志位更新?? if cfg.flag_audio_cheat is True: # 播放音频 # 清屏 au.play_aud_dirc('cheat') gui.clean() cfg.flag_audio_cheat = False pass elif cfg.stage == 1: # 1.晚上:狼人行动 if cfg.flag_jump is True: # 跳转页面 au_background.play_aud_dirc('bgm') au_background.player.setVolume(30) # 背景乐开始播放 gui.jump_werewolf() app.processEvents() cfg.flag_jump = False if cfg.flag_audio_start is True: # 播放狼人开始提示音 au.play_aud_dirc('werewolves_open') cfg.flag_audio_start = False processor.canopen_list = game.spec_id['Werewolf'] # 狼人可以睁眼 if processor.check_cheat() == 0: # 无人作弊 """更新 or 锁定""" if cfg.flag_confirm is False: # 玩家尚未确认, 更新页面 gui.update_werewolf(str(processor.get_voting_digit(game.spec_id['Werewolf'])), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() # 累积手势 processor.add_posture(processor.get_voting_digit(game.spec_id['Werewolf'])) # 添加当前帧结果 if processor.accumulate_pro() is True: # 与上一帧比较相同 cfg.confirm_pro += cfg.comfirm_rate else: cfg.confirm_pro = 0 # 清空累积 # 倒计时 if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate else: # 锁定:保证流程走完再结束 gui.update_werewolf(str(processor.get_voting_digit(game.spec_id['Werewolf'])), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) # 锁定,只更新倒计时 app.processEvents() if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate """确认条件""" # 1.累积确认 if cfg.confirm_pro >= cfg.comfirm_thres and cfg.flag_confirm is False: # 确认杀人 if processor.get_voting_digit(game.spec_id['Werewolf']) <= cfg.num_players: # 狼人要杀的人在玩家里面 game.movement_werewolf(cfg.day, killed_id=processor.get_voting_digit(game.spec_id['Werewolf'])) # 更新游戏逻辑 cfg.flag_confirm = True # 继续倒计时 else: # 没有杀人 gui.update_werewolf(str(0), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() game.movement_werewolf(cfg.day, killed_id=0) # 更新游戏逻辑 cfg.flag_confirm = True # 继续倒计时 # 2.倒计时结束 elif cfg.countdown_pro <= 0 and cfg.flag_confirm is False: # 倒计时结束, 玩家尚未确认,取最后一帧的结果 if processor.get_voting_digit(game.spec_id['Werewolf']) <= cfg.num_players: # 狼人要杀的人在玩家里面 game.movement_werewolf(cfg.day, killed_id=processor.get_voting_digit(game.spec_id['Werewolf'])) # 更新游戏逻辑 au.play_aud_dirc('werewolves_close') reset_flag() else: # 没有杀人 gui.update_werewolf(str(0), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() game.movement_werewolf(cfg.day, killed_id=0) au.play_aud_dirc('werewolves_close') reset_flag() elif cfg.countdown_pro <= 0 and cfg.flag_confirm is True: # 倒计时结束, 玩家确认 au.play_aud_dirc('werewolves_close') reset_flag() else: pass # 继续循环 elif processor.check_cheat() != 0: # 有人睁眼 # 加:提示页面和提示语音 print('有人作弊!', processor.check_cheat()) pass elif cfg.stage == 2: # 2.晚上:预言家行动 if cfg.flag_jump is True: # 跳转页面 gui.jump_seer() app.processEvents() cfg.flag_jump = False if cfg.flag_audio_start is True: # 预言家睁眼提示音 time.sleep(6) # 等待上一段音频播放完成 au.play_aud_dirc('seer_open') time.sleep(3) # 等待语音播放一点 cfg.flag_audio_start = False processor.canopen_list = game.spec_id['Seer'] # 预言家可以睁眼 if processor.check_cheat() == 0: # 无人作弊 """更新 or 锁定""" if cfg.flag_confirm is False: # 更新 gui.update_seer(str(processor.get_voting_digit(game.spec_id['Seer'])), 2, cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) # 直到预言家确认才能给出验人状态 app.processEvents() # 累积手势 processor.add_posture(processor.get_voting_digit(game.spec_id['Seer'])) # 添加当前帧结果 if processor.accumulate_pro() is True: # 与上一帧比较相同 cfg.confirm_pro += cfg.comfirm_rate else: cfg.confirm_pro = 0 # 清空累积 # 倒计时 if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate else: # 锁定,冻结页面,预言家看到结果 gui.update_seer(str(processor.get_voting_digit(game.spec_id['Seer'])), game.movement_seer(processor.get_voting_digit(game.spec_id['Seer'])), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() # 倒计时 if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate """确认条件""" # 1.累积确认 if cfg.confirm_pro >= cfg.comfirm_thres and cfg.flag_confirm is False: # 告诉预言家结果 if processor.get_voting_digit(game.spec_id['Seer']) <= cfg.num_players: # 预言家验人结果再游戏玩家里面 gui.update_seer(str(processor.get_voting_digit(game.spec_id['Seer'])), game.movement_seer(processor.get_voting_digit(game.spec_id['Seer'])), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() cfg.flag_confirm = True # 继续倒计时 else: # 否则不验人 gui.update_seer(str(processor.get_voting_digit(game.spec_id['Seer'])), game.movement_seer(0), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) # 预测超过游戏id则不确定 app.processEvents() cfg.flag_confirm = True # 继续倒计时 # 2.倒计时结束 elif cfg.countdown_pro <= 0 and cfg.flag_confirm is False: # 倒计时结束, 玩家尚未确认,取最后一帧的结果 if processor.get_voting_digit(game.spec_id['Seer']) <= cfg.num_players: # 预言家验人结果再游戏玩家里面 gui.update_seer(str(processor.get_voting_digit(game.spec_id['Seer'])), game.movement_seer(processor.get_voting_digit(game.spec_id['Seer'])), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() au.play_aud_dirc('seer_close') reset_flag() else: # 否则不验人 gui.update_seer(str(processor.get_voting_digit(game.spec_id['Seer'])), game.movement_seer(0), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) # 预测超过游戏id则不确定 app.processEvents() au.play_aud_dirc('seer_close') reset_flag() elif cfg.countdown_pro <= 0 and cfg.flag_confirm is True: # 倒计时结束, 玩家确认 au.play_aud_dirc('seer_close') reset_flag() else: pass # 跳出循环 elif processor.check_cheat() != 0: # 有人睁眼 # 加:提示页面和提示语音 print('有人作弊!', processor.check_cheat()) pass elif cfg.stage == 3: # 3.晚上:女巫救人 if cfg.flag_jump is True: # 跳转页面 time.sleep(4) # 等待预言家播放完音频 gui.jump_witch_save() app.processEvents() cfg.flag_jump = False if cfg.flag_audio_start is True: # au_background.play_aud_dirc('bgm') # 再次播放背景音频 au.play_aud_dirc('witch_save_open') cfg.flag_audio_start = False processor.canopen_list = game.spec_id['Witch'] # 女巫可以睁眼 if processor.check_cheat() == 0: # 无人作弊 """更新 or 锁定""" if cfg.flag_confirm is False: # 玩家尚未确认, 更新页面 gui.witch_save(game.progress[cfg.day][0], processor.get_thumbs(game.spec_id['Witch']), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() # 累积手势 processor.add_posture(processor.get_thumbs(game.spec_id['Witch'])) # 添加当前帧结果 if processor.accumulate_pro() is True: # 与上一帧比较相同 cfg.confirm_pro += cfg.comfirm_rate else: cfg.confirm_pro = 0 # 清空累积 if cfg.countdown_pro > 0: # 倒计时 cfg.countdown_pro -= cfg.countdown_rate else: # 锁定:保证流程走完再结束 gui.witch_save(game.progress[cfg.day][0], processor.get_thumbs(game.spec_id['Witch']), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate """确认条件""" # 1.累积确认 if cfg.confirm_pro >= cfg.comfirm_thres and cfg.flag_confirm is False: # 确认杀人 gui.witch_save(game.progress[cfg.day][0], processor.get_thumbs(game.spec_id['Witch']), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() if processor.get_thumbs(game.spec_id['Witch']) == 2: # 救人 game.movement_witch_save(cfg.day, 1) else: # 不确定或反对为不救人 game.movement_witch_save(cfg.day, 0) # 不救人 cfg.flag_confirm = True # 继续倒计时 # 2.倒计时结束 elif cfg.countdown_pro <= 0 and cfg.flag_confirm is False: # 倒计时结束, 玩家尚未确认,取最后一帧的结果 # 冻结最后的结果 gui.witch_save(game.progress[cfg.day][0], processor.get_thumbs(game.spec_id['Witch']), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() if processor.get_thumbs(game.spec_id['Witch']) == 2: # 救人 game.movement_witch_save(cfg.day, 1) else: # 不确定或反对为不救人 game.movement_witch_save(cfg.day, 0) # 不救人 reset_flag() elif cfg.countdown_pro <= 0 and cfg.flag_confirm is True: # 倒计时结束, 玩家确认 reset_flag() else: pass elif processor.check_cheat() != 0: # 有人睁眼 # 加:提示页面和提示语音 print('有人作弊!', processor.check_cheat()) pass elif cfg.stage == 4: # 4.晚上:女巫毒人 if cfg.flag_jump is True: # 跳转页面 gui.jump_witch_poison() cfg.flag_jump = False if cfg.flag_audio_start is True: au.play_aud_dirc('witch_poison_open') cfg.flag_audio_start = False processor.canopen_list = game.spec_id['Witch'] # 女巫可以睁眼 if processor.check_cheat() == 0: # 无人作弊 """更新 or 锁定""" if cfg.flag_confirm is False: # 玩家尚未确认, 更新页面 gui.witch_poison(str(processor.get_voting_digit(game.spec_id['Witch'])), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() # 累积手势 processor.add_posture(processor.get_voting_digit(game.spec_id['Witch'])) # 添加当前帧结果 if processor.accumulate_pro() is True: # 与上一帧比较相同 cfg.confirm_pro += cfg.comfirm_rate else: cfg.confirm_pro = 0 # 清空累积 if cfg.countdown_pro > 0: # 倒计时 cfg.countdown_pro -= cfg.countdown_rate else: # 锁定:保证流程走完再结束 gui.witch_poison(str(processor.get_voting_digit(game.spec_id['Witch'])), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate """确认条件""" # 1.累积确认 if cfg.confirm_pro >= cfg.comfirm_thres and cfg.flag_confirm is False: # 确认毒人 if processor.get_voting_digit(game.spec_id['Witch']) <= cfg.num_players: # 女巫要毒的人在玩家里面 game.movement_witch_poison(cfg.day, processor.get_voting_digit(game.spec_id['Witch'])) # 更新游戏逻辑 cfg.flag_confirm = True # 继续倒计时 else: # 没有毒人 gui.witch_poison(str(0), cfg.confirm_pro, cfg.countdown_pro) app.processEvents() game.movement_witch_poison(cfg.day, 0) # 不毒人 cfg.flag_confirm = True # 继续倒计时 # 2.倒计时结束 elif cfg.countdown_pro <= 0 and cfg.flag_confirm is False: # 倒计时结束, 玩家尚未确认,取最后一帧的结果 if processor.get_voting_digit(game.spec_id['Witch']) <= cfg.num_players: # 女巫要毒的人在玩家里面 game.movement_witch_poison(cfg.day, processor.get_voting_digit(game.spec_id['Witch'])) # 更新游戏逻辑 au.play_aud_dirc('witch_close') reset_flag() else: # 没有毒人 gui.witch_poison(str(0), cfg.confirm_pro, cfg.countdown_pro, cfg.flag_confirm) app.processEvents() game.movement_witch_poison(cfg.day, 0) # 不毒人 au.play_aud_dirc('witch_close') reset_flag() elif cfg.countdown_pro <= 0 and cfg.flag_confirm is True: # 倒计时结束, 玩家确认 au.play_aud_dirc('witch_close') reset_flag() else: pass elif processor.check_cheat() != 0: # 有人睁眼 # 加:提示页面和提示语音 print('有人作弊!', processor.check_cheat()) pass elif cfg.stage == 5: # 5.天亮,宣布死讯 if cfg.flag_jump is True: # 跳转页面 gui.jump_announce_night() app.processEvents() cfg.flag_jump = False """宣布死讯""" # 更新睁眼列表?不需要作弊检测 # 播放音频 if cfg.flag_audio_start is True: time.sleep(3) if game.announce_night(cfg.day)[0] == 0 and game.announce_night(cfg.day)[1] == 0: # 平安夜 au.play_aud_dirc('report_nodied') elif game.announce_night(cfg.day)[0] == game.spec_id['Hunter'][0]: # 猎人被杀 au.play_aud_dirc('report_hunterdied') else: # 有人被杀 au.play_aud_dirc('report_somedied') cfg.flag_audio_start = False gui.announce_night(cfg.day, game.announce_night(cfg.day)[0], game.announce_night(cfg.day)[1]) app.processEvents() time.sleep(10) # 等待玩家看结果 """判断游戏流程""" if game.announce_night(cfg.day)[0] == game.spec_id['Hunter'][0] and game.spec_id['Hunter'][ 0] != 0: # 首先判断猎人有没有被杀 + 板子里面有猎人 reset_flag() cfg.stage = 8 # 跳转到猎人 elif game.is_gameover(game.get_character_list()) == 0: # 游戏继续 reset_flag() else: # 游戏结束 reset_flag() cfg.stage = 9 elif cfg.stage == 6: # 6.天亮:讨论 if cfg.flag_jump is True: # 跳转页面 gui.jump_status() app.processEvents() cfg.flag_jump = False if cfg.flag_audio_start is True: au.play_aud_dirc('discussion') au_background.player.setVolume(0) # 关闭背景乐 cfg.flag_audio_start = False gui.status(game.get_status_list()) # 更新玩家状态表 app.processEvents() """判断所有人的大拇指, 所有人大拇指向上则开始voting流程""" # 累积手势 if processor.get_thumbs(game.get_alive_idlist()) == 2: # 大部分人比大拇指 cfg.confirm_pro += cfg.comfirm_rate else: cfg.confirm_pro = 0 # 清空累积 """确认条件""" # 1.累积确认 if cfg.confirm_pro >= cfg.comfirm_thres: # 确认开始投票 reset_flag() # 下一个流程 elif cfg.stage == 7: # 7.天亮:投票 if cfg.flag_jump is True: # 跳转页面 gui.jump_vote() app.processEvents() cfg.flag_jump = False if cfg.flag_audio_start is True: au.play_aud_dirc('vote') time.sleep(5) cfg.flag_audio_start = False """开始投票""" gui.vote(processor.voting_day(game.get_alive_idlist()), cfg.confirm_pro, cfg.countdown_pro) app.processEvents() # 累积手势 processor.add_posture(processor.voting_day(game.get_alive_idlist())) # 添加当前帧结果 if processor.accumulate_pro() is True: # 与上一帧比较相同 cfg.confirm_pro += cfg.comfirm_rate else: cfg.confirm_pro = 0 # 清空累积 # 倒计时 if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate """确认条件""" # 1.累积确认 2.倒计时结束 if cfg.confirm_pro >= cfg.comfirm_thres or cfg.countdown_pro <= 0: # 确认投票 if cfg.num_players >= processor.voting_day(game.get_alive_idlist()) >= 0: # 要投的人在玩家里面 + 没有平票 game.voting(cfg.day, processor.voting_day(game.get_alive_idlist())) # 更新游戏逻辑 if game.is_gameover(game.get_character_list()) == 0: # 游戏继续 if processor.voting_day(game.get_alive_idlist()) == game.spec_id['Hunter'][0]: # 猎人被投 reset_flag() cfg.stage = 8 # 进入猎人流程 else: au.play_aud_dirc('dark_close') # 准备进入晚上 reset_flag() cfg.stage = 1 cfg.day += 1 time.sleep(7) else: # 游戏结束 reset_flag() cfg.stage = 9 elif processor.voting_day(game.get_alive_idlist()) == -1: # 有人平票 au.play_aud_dirc('vote_equal') reset_flag() cfg.stage = 7 # 重新投票 time.sleep(7) else: pass elif cfg.stage == 8: # 8 特殊: 猎人开枪 if cfg.flag_jump is True: # 跳转页面 gui.jump_hunter() app.processEvents() cfg.flag_jump = False if cfg.flag_audio_start is True: au.play_aud_dirc('hunter_open') time.sleep(3) cfg.flag_audio_start = False """更新""" gui.hunter(str(processor.get_voting_digit(game.spec_id['Hunter'])), cfg.confirm_pro, cfg.countdown_pro) app.processEvents() # 累积手势 processor.add_posture(processor.get_voting_digit(game.spec_id['Hunter'])) # 添加当前帧结果 if processor.accumulate_pro() is True: # 与上一帧比较相同 cfg.confirm_pro += cfg.comfirm_rate else: cfg.confirm_pro = 0 # 清空累积 # 倒计时 if cfg.countdown_pro > 0: cfg.countdown_pro -= cfg.countdown_rate """确认条件""" if cfg.confirm_pro >= cfg.comfirm_thres or cfg.countdown_pro <= 0: # 确认开枪 if cfg.num_players >= processor.voting_day(game.get_alive_idlist()): # 要开枪的人在玩家里 game.movement_hunter(processor.get_voting_digit(game.spec_id['Hunter'])) # 更新游戏逻辑: 猎人开枪,0为不开枪 au.play_aud_dirc('hunter_end') time.sleep(3) if game.is_gameover(game.get_character_list()) == 0: # 游戏继续 if game.players[game.spec_id['Hunter'][0]].status == 'killed': # 猎人被杀 reset_flag() cfg.stage = 6 # 白天讨论 time.sleep(1) elif game.players[game.spec_id['Hunter'][0]].status == 'voted': # 猎人被投票出局 au.play_aud_dirc('dark_close') # 准备进入晚上 reset_flag() cfg.stage = 1 cfg.day += 1 cfg.flag_audio_background = True # 继续播放背景乐 time.sleep(7) else: print('error: 猎人还魂了!') break else: # 游戏结束 reset_flag() cfg.stage = 9 else: reset_flag() cfg.stage = 6 # 白天讨论 time.sleep(1) else: pass elif cfg.stage == 9: # 9. 结束:游戏结束 if cfg.flag_jump is True: # 跳转页面 if game.is_gameover(game.get_character_list()) == 1: # 好人获胜 gui.jump_gameover(1) elif game.is_gameover(game.get_character_list()) == 2: # 坏人获胜 gui.jump_gameover(2) else: print('error: 一个不存在的人获得了胜利') app.processEvents() cfg.flag_jump = False if cfg.flag_audio_start is True: if game.is_gameover(game.get_character_list()) == 1: # 好人获胜 au.play_aud_dirc('villager_win') elif game.is_gameover(game.get_character_list()) == 2: # 坏人获胜 au.play_aud_dirc('werewolf_win') else: print('error: 一个不存在的人获得了胜利') cfg.flag_audio_start = False time.sleep(10) cfg.stage = -1 elif cfg.stage == -1: # 游戏结束:跳出程序 if cfg.flag_audio_start is True: au.play_aud_dirc('thank') time.sleep(11) cfg.flag_audio_start = False print('alive list: ', game.get_status_list()) print('this is the game progress{day: [killed_id, poisoned_id, flag_save,]:', game.progress) print('Thank you for playing our game!') break """GUI 刷新""" app.processEvents() # GUI刷新 # test visual img new_frame = yoloall.vis(frame, boxs, confidence, class_names) # 存储 if writeVideo_flag: out.write(frame) """测试用:预测结果可视化输出""" fps = (fps + (1. / (time.time() - t1))) / 2 cv2.putText(new_frame, "FPS: %f" % (fps), (int(20), int(40)), 0, 5e-3 * 200, (145, 145, 145), 2) cv2.namedWindow("YOLO4_Deep_SORT", 0) # cv2.resizeWindow('YOLO4_Deep_SORT', 1024, 768) cv2.resizeWindow('YOLO4_Deep_SORT', w, h) cv2.imshow('YOLO4_Deep_SORT', new_frame) # Press Q to stop! if cv2.waitKey(1) & 0xFF == ord('q'): break # exit system if writeVideo_flag: out.release() cv2.destroyAllWindows() video_capture.release()
def main(yolo): os.chdir('..') send_to_GUI = 0 video_record = 1 source = 'RPi' # 0 for webcam or RPi or filename FLAGScsv = 0 dict_prof = {} if FLAGScsv: csv_obj = save_csv() id_stay_old = [[], []] colors = { "male": (0, 0, 255), "female": (255, 0, 0), "None": (255, 255, 255) } device_obj = device_register() if send_to_GUI: # send video to note's GUI gst_out = cv2.VideoWriter( 'appsrc ! videoconvert ! jpegenc quality=12 ! tcpserversink host=0.0.0.0 port=6007 sync=false', 0, 15, (416, 416)) # Definition of the parameters max_cosine_distance = 1.5 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'deep_sort/model_data/mars-small128.pb' encoder = gdet.create_box_encoder(model_filename, batch_size=8) metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric, max_iou_distance=0.7, max_age=50, n_init=3, _next_id=1) if source == 'RPi': video_capture = connect_RPi() else: video_capture = cv2.VideoCapture(source) video_capture.set(cv2.CAP_PROP_BUFFERSIZE, 1) print('video source : ', source) if video_record: out = cv2.VideoWriter() out.open('output.mp4', cv2.VideoWriter_fourcc(*'H264'), 25, (1920, 1080), True) # ___________________________________________________________________________________________________________________________________________MAIN LOOP t_fps = [time.time()] while True: for i in range(round(20 / 8)): video_capture.grab() ret, frame = video_capture.read() if not ret: if source == 'RPi': print('[ INFO ] No frame received from RPi: wait for 5 sec') time.sleep(5) video_capture = connect_RPi() continue else: video_capture = cv2.VideoCapture(source) video_capture.set(cv2.CAP_PROP_BUFFERSIZE, 1) continue frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # ______________________________________________________________________________________________________________________________DETECT WITH YOLO [gen_things, dev_things ] = yolo.detect_image(frame, boxes_only=True) # main detect function HERE features_gen = encoder(frame, gen_things[0]) detections_gen = [ Detection(bbox, 1.0, feature_gen) for bbox, feature_gen in zip(gen_things[0], features_gen) ] features_dev = encoder(frame, dev_things[0]) detections_dev = [ Detection(bbox, 1.0, feature_dev) for bbox, feature_dev in zip(dev_things[0], features_dev) ] device_obj.startframe(detections_dev) # ______________________________________________________________________________________________________________________________DRAW DEVICE for i in range(0, len(detections_dev)): bbox = detections_dev[i].to_tlbr() label = dev_things[1][i] cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2) cv2.putText(frame, label, (int(bbox[0]), int(bbox[1]) + 30), cv2.FONT_HERSHEY_SIMPLEX, 5e-3 * 200, (255, 0, 0), 2) # ______________________________________________________________________________________________________________________________Call the tracker tracker.predict() tracker.update(detections_gen, gen_things[1]) # feed detections # __________________________________________________________________________________________________________________________DRAW TRACK RECTANGLE id_stay = [[], []] for track in tracker.tracks: #dev_1p = {track.track_id:None} if track.is_confirmed() and track.time_since_update > 1: continue bbox = track.to_tlbr() #(min x, miny, max x, max y) bcenter = track.to_xyah( ) #(center x, center y, aspect ratio,height) dict_prof[track.track_id] = [[str(track.gender)], []] # check device if (len(detections_dev) != 0) and (len(detections_gen) != 0): # detected some thing euc_1p = device_obj.update_person(bcenter, track.track_id) for connect in euc_1p: #each person if connect is not None: cv2.line(frame, (int(bcenter[0]), int(bcenter[1])), (int(connect[1]), int(connect[2])), (0, 255, 0), 3) device_label = dev_things[1][int(connect[0])] if device_label not in dict_prof[ track.track_id]: # not write the same device dict_prof[track.track_id][1].append(device_label) if track.gender == 'male': # Avoid None id_stay[0].append(track.track_id) dict_prof[track.track_id][0] = ['male'] if track.gender == 'female': id_stay[1].append(track.track_id) dict_prof[track.track_id][0] = ['female'] cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), colors[str(track.gender)], 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) cv2.putText(frame, str(track.gender), (int(bbox[0]), int(bbox[1]) + 70), cv2.FONT_HERSHEY_SIMPLEX, 5e-3 * 200, (0, 255, 0), 3) frame = cv2.cvtColor( frame, cv2.COLOR_RGB2BGR) #change to BGR for showing with OpenCV # __________________________________________________________________________________________________________________________ FRAME RATE things t_fps.append(time.time()) fps = 1 / (t_fps[1] - t_fps[0]) t_fps.pop(0) cv2.putText(frame, 'FPS : {:.2f}'.format(fps), (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 5e-3 * 100, (0, 0, 255), 2) out.write( frame) if video_record else None # write frame if record to file if send_to_GUI: frame = cv2.resize(frame, (416, 416)) gst_out.write(frame) print('FPS : {:.2f}'.format(fps)) else: cv2.imshow('', frame) if (id_stay != id_stay_old ) and FLAGScsv: # save csv if people in frame have changed csv_obj.save_event(id_stay) if cv2.waitKey(1) & 0xFF == ord('q'): break id_stay_old = id_stay out.release() if video_record else None gst_out.release() if send_to_GUI else None video_capture.release() cv2.destroyAllWindows() if FLAGScsv: csv_obj.save_profile(dict_prof)
def merge_pdf(*args): # Получаем список полных путей к указанным страницам в папке merge pdf_glob = glob.glob(BASE_PATH + '/merge/*.pdf') # Создаем путь к файлу типа (600, 673).pdf new_merge_pdf = Path('merge/' f"{args}" '.pdf') # Создаем экземпляр класса merge_pdfs = PdfFileMerger() for i in natsorted(pdf_glob): # Каждый файл открываем, читаем и добавляем в сортированном порядке merge_pdfs.append(i) # Записываем в новый файл список merge_pdfs merge_pdfs.write(open(new_merge_pdf, 'wb')) merge_pdfs.close() print(f'Файл {args} создан!') shutil.move(f'{BASE_PATH}/merge/{args}.pdf', f'{BASE_PATH}/create/{args}.pdf') split_pdf_pages(66, 99) time.sleep(5) split_pdf_pages(301, 400) time.sleep(5) split_pdf_pages(501, 600)
def main(yolo, hide_window, weights_file, test_mode, test_output, bayesian, batch_factor, input_file, progress_recorder): if test_mode: global object_detection_file global object_tracking_directory global action_recognition_directory initialiseTestMode(test_output) print('Starting pipeline...') # Define output path based on file name and web_server dir file_name = os.path.splitext(os.path.basename(input_file))[0] #output_file = f'./web_server/output_{file_name}.avi' #output_file = f'./gui/webapp/static/output_{file_name}.avi' # Existing pipeline version. Commented for easier Django display. output_file = f'./gui/webapp/static/output.mp4' ### # Definition of the parameters max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 model = TS_CNN_LSTM(INPUT_SHAPE, CLASSES) model.load_weights('action_recognition/architectures/weights/' + weights_file + '.hdf5') # Track id frame batch track_tubeMap = {} # Track id action track_actionMap = {} # Image data generator datagen = ImageDataGenerator() # deep_sort model_filename = 'object_detection/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) ################################################################################ # New: Count total number of frames! video_count = cv2.VideoCapture(input_file) frame_total = int(video_count.get(cv2.CAP_PROP_FRAME_COUNT)) print("FRAME COUNT: ", frame_total) video_count.release() ################################################################################ video_capture = FileVideoStream(input_file).start() # Let input stream load some frames time.sleep(5) # Define the codec and create VideoWriter object w = 3840 h = 2160 #fourcc = cv2.VideoWriter_fourcc(*'MJPG') #*'XVID' fourcc = cv2.VideoWriter_fourcc(*'H264') # Now mp4! # Build video output handler only if we are not cropping out = None if not test_mode: out = cv2.VideoWriter(output_file, fourcc, 11, (w, h)) fps = 0.0 location = (0, 0) frame_number = 0 track_buffer = [] unprocessedFrames = [] processedFrames = [] processedTracks = [] locations = [] skip = 1 while video_capture.more(): #### frame = video_capture.read() # frame shape 640*480*3 if not isinstance(frame, np.ndarray): break t1 = time.time() x = w y = h scaledX = 640 scaledY = 360 xScale = x / scaledX yScale = y / scaledY if (frame_number % skip == 0): if (not location) or frame_number % 5 == 0: location = [0, 0, scaledX, scaledY] else: location = rescale(location, xScale, yScale, 0, 0, False) frameCopy = frame.copy() frame = cv2.resize(frame, (scaledX, scaledY), interpolation=cv2.INTER_AREA) # image = Image.fromarray(frame) image = Image.fromarray(frame[..., ::-1]) #bgr to rgb diffx = location[0] diffy = location[1] image = image.crop( (location[0], location[1], location[2], location[3])) boxs = yolo.detect_image(image) features = encoder(frame, boxs) # score to 1.0 here). detections = [ Detection(bbox, 1.0, feature).rescale(xScale, yScale, diffx, diffy) 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 test_mode: if len(detections) != 0: for i in range(0, len(detections)): bbox = detections[i].to_tlbr() object_detection_file.write( str(frame_number) + ' ' + str(int(bbox[0])) + ' ' + str(int(bbox[1])) + ' ' + str(int(bbox[2])) + ' ' + str(int(bbox[3])) + '\n') # Call the tracker tracker.predict() tracker.update(detections) frame = frameCopy tracks = dict() for track in tracker.tracks: if not track.is_confirmed() or track.time_since_update > 1: continue bbox = track.to_tlbr() tracks[track.track_id] = bbox if frame_number == 0: track_buffer.append(tracks) processedFrames.append(frame) processedTracks.append(tracks) else: firstTrack = track_buffer.pop(0) secondTrack = tracks.copy() for i, oldFrame in enumerate(unprocessedFrames, 1): tracks = dict() for trackId in secondTrack: if trackId in firstTrack: min_x, min_y, max_x, max_y = interpolateBbox( firstTrack[trackId], secondTrack[trackId], i / skip) result = [ x + y for x, y in zip([min_x, min_y, max_x, max_y], firstTrack[trackId]) ] tracks[trackId] = result processedFrames.append(oldFrame) processedTracks.append(tracks.copy()) unprocessedFrames = [] processedFrames.append(frame) processedTracks.append(secondTrack) track_buffer.append(secondTrack) location = rescale(location, xScale, yScale, 0, 0, True) locations.append(location) if (frame_number >= skip): frame = processFrame(locations, processedFrames, processedTracks, track_tubeMap, track_actionMap, model, test_mode, bayesian, batch_factor) currentXs = [] currentYs = [] for det in detections: bbox = det.to_tlbr() currentXs.extend([int(bbox[0]), int(bbox[2])]) currentYs.extend([int(bbox[1]), int(bbox[3])]) else: unprocessedFrames.append(frame) locations.append([0, 0, 0, 0]) if (frame_number >= skip): frame = processFrame(locations, processedFrames, processedTracks, track_tubeMap, track_actionMap, model, test_mode, bayesian, batch_factor) # Display video as processed if necessary # save a frame if (frame_number >= skip): writeFrame(frame, out, hide_window, test_mode) frame_number += 1 # Updates progress bar every 10 frames if frame_number % 10 == 0: progress_recorder.set_progress(frame_number, frame_total) #### if frame_number % 5 != 0: location = calculateLocation(currentXs, currentYs) fps = (fps + (1. / (time.time() - t1))) / 2 print("fps= %f" % (fps / skip)) # Press Q to stop! if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.stop() while len(processedFrames) != 0: frame_number += 1 frame = processFrame(locations, processedFrames, processedTracks, track_tubeMap, track_actionMap, model, test_mode, bayesian, batch_factor) writeFrame(frame, out, hide_window, test_mode) while len(unprocessedFrames) != 0: frame_number += 1 frame = unprocessedFrames.pop() writeFrame(frame, out, hide_window, test_mode) if not test_mode: out.release() cv2.destroyAllWindows() if test_mode: object_detection_file.close()
# remove tmps if not set if options['save_tmp'] is False: try: os.rmdir(options['tmp_folder']) os.rmdir( os.path.join(options['current_folder'], options['experiment'])) except: pass print("> INFO: All processes have been finished.") if __name__ == '__main__': # try: print('') print('') print('\x1b[6;30;42m' + 'inferencing started.......................' + '\x1b[0m') options = get_config() infer_segmentation(options) print('\x1b[6;30;42m' + 'inferencing completed.....................' + '\x1b[0m') except KeyboardInterrupt: print("KeyboardInterrupt has been caught.") time.sleep(1) os.kill(os.getpid(), signal.SIGTERM)
queue_name = 'processes_'+str(processes) queue_name = manager.Queue() # Queue first, then detection (Obj 2) # Process for Queueing, to send over name of Queue and data source p = Process(target=start_queue,args=(queue_name,data[i])) # Process for detection, to send over Queue name, amount of processes, initial_id and r_id lists for re-identification, # detected cam_id and unique_ids list p1 = Process(target=main,args=(queue_name,processes,initial_id,r_id,cam_id,unique_id)) process_capture.append(p) process_read.append(p1) processes+=1 #Assign a new camera camID (increment) for the next camera source to be read #Start Processses for p in process_capture: p.start() time.sleep(1)# Delay for frames to be read first as the reading of the RTSP streaming will be slower. for p in process_read: p.start() #To handle the "zombie" state where the parent (the main program) process terminate while the child process is still alive. #By joining the two processes, the termination of one will lead to the termination of the other. for p in process_read: p.join() # Join parent process to child process (For parent process to wait for child process to end before terminating) #In event when more than 1 camera is available, the reid will be run, thus this is to end the reid process. if rp.is_alive(): flag.set() # Set flag to indicate re-identification process to end # Write re-identifications onto text file with unique IDs rid_file = open('logs/reid.txt', 'w') for i in range(len(initial_id)):