diff --git a/AI_ENGINE/DATA/CON.mp4 b/AI_ENGINE/DATA/CON.mp4 old mode 100644 new mode 100755 diff --git a/AI_ENGINE/DATA/Trim.mp4 b/AI_ENGINE/DATA/Trim.mp4 new file mode 100644 index 0000000..254a40a Binary files /dev/null and b/AI_ENGINE/DATA/Trim.mp4 differ diff --git a/AI_ENGINE/DATA/WD.mp4 b/AI_ENGINE/DATA/WD.mp4 new file mode 100644 index 0000000..86573b6 Binary files /dev/null and b/AI_ENGINE/DATA/WD.mp4 differ diff --git a/AI_ENGINE/DATA/WD_crop.mp4 b/AI_ENGINE/DATA/WD_crop.mp4 new file mode 100644 index 0000000..edecc15 Binary files /dev/null and b/AI_ENGINE/DATA/WD_crop.mp4 differ diff --git a/AI_ENGINE/DATA/ftp_data/bi.jpg b/AI_ENGINE/DATA/ftp_data/bi.jpg deleted file mode 100644 index 31a033f..0000000 Binary files a/AI_ENGINE/DATA/ftp_data/bi.jpg and /dev/null differ diff --git a/AI_ENGINE/DATA/ftp_data/con_setup.jpg b/AI_ENGINE/DATA/ftp_data/con_setup.jpg index 3f18f8f..7ddf088 100644 Binary files a/AI_ENGINE/DATA/ftp_data/con_setup.jpg and b/AI_ENGINE/DATA/ftp_data/con_setup.jpg differ diff --git a/AI_ENGINE/DATA/ftp_data/fr.jpg b/AI_ENGINE/DATA/ftp_data/fr.jpg index b9a8b33..21669e3 100644 Binary files a/AI_ENGINE/DATA/ftp_data/fr.jpg and b/AI_ENGINE/DATA/ftp_data/fr.jpg differ diff --git a/AI_ENGINE/DATA/ftp_data/local.jpg b/AI_ENGINE/DATA/ftp_data/local.jpg deleted file mode 100644 index b0e0d8c..0000000 Binary files a/AI_ENGINE/DATA/ftp_data/local.jpg and /dev/null differ diff --git a/AI_ENGINE/DATA/ftp_data/ppe.jpg b/AI_ENGINE/DATA/ftp_data/ppe.jpg index 08a3ae7..d653dd1 100644 Binary files a/AI_ENGINE/DATA/ftp_data/ppe.jpg and b/AI_ENGINE/DATA/ftp_data/ppe.jpg differ diff --git a/AI_ENGINE/DATA/ftp_data/wd.jpg b/AI_ENGINE/DATA/ftp_data/wd.jpg index d93b6d5..a2b7ee1 100644 Binary files a/AI_ENGINE/DATA/ftp_data/wd.jpg and b/AI_ENGINE/DATA/ftp_data/wd.jpg differ diff --git a/AI_ENGINE/demo_utils.py b/AI_ENGINE/demo_utils.py index 0f29496..49d2732 100644 --- a/AI_ENGINE/demo_utils.py +++ b/AI_ENGINE/demo_utils.py @@ -42,7 +42,7 @@ def _bi_sftp_upload(): remotepath = AI_CONST.FTP_LOCATION + os.sep + AI_CONST.FTP_BI_FILE_NAME + '.jpg' - #sftp.put(AI_CONST.FTP_BI_RESULT, remotepath) + sftp.put(AI_CONST.FTP_BI_RESULT, remotepath) sftp.close() transprot.close() diff --git a/AI_ENGINE/mqtt_publish.py b/AI_ENGINE/mqtt_publish.py index 1258a71..f8834ee 100644 --- a/AI_ENGINE/mqtt_publish.py +++ b/AI_ENGINE/mqtt_publish.py @@ -42,3 +42,6 @@ client.on_disconnect = on_disconnect client.username_pw_set(AI_CONST.MQTT_USER_ID,AI_CONST.MQTT_USER_PW) # address : localhost, port: 1883 에 연결 client.connect(AI_CONST.MQTT_HOST, AI_CONST.MQTT_PORT) + + + diff --git a/AI_ENGINE/mqtt_subscribe.py b/AI_ENGINE/mqtt_subscribe.py index 7a60bbc..eefe74f 100644 --- a/AI_ENGINE/mqtt_subscribe.py +++ b/AI_ENGINE/mqtt_subscribe.py @@ -56,9 +56,4 @@ client.username_pw_set(AI_CONST.MQTT_USER_ID, AI_CONST.MQTT_USER_PW) client.connect(AI_CONST.MQTT_HOST, AI_CONST.MQTT_PORT) client.subscribe(AI_CONST.MQTT_PPE_TOPIC) # ppe client.subscribe(AI_CONST.MQTT_FR_TOPIC) # FR - - - client.loop_forever() - - diff --git a/DL/FR/d2_face_detect.py b/DL/FR/d2_face_detect.py index 896be9a..225dcfa 100644 --- a/DL/FR/d2_face_detect.py +++ b/DL/FR/d2_face_detect.py @@ -120,7 +120,7 @@ class FaceDetect: remotepath = self.ftp_info.location + os.sep + self.ftp_info.file_face + '.jpg' - #sftp.put(AI_CONST.FTP_FR_RESULT, remotepath) + sftp.put(AI_CONST.FTP_FR_RESULT, remotepath) sftp.close() transprot.close() @@ -155,6 +155,7 @@ class FaceDetect: def _result_update(self, matched_list: list): """ + :param matched_list: detect 된 object list """ self.mqtt_status = self.thread_value.STATUS_NONE # status init @@ -330,8 +331,14 @@ class FaceDetect: 'encoding' : face_recognition.face_encodings(worker22_image)[0] } + worker100_image = face_recognition.load_image_file(AI_CONST.WORKER100_IMG_PATH) + no000100 = { + 'target_names' : 'no000100', + 'encoding' : face_recognition.face_encodings(worker100_image)[0] + } + encoding_list = [ - #no000001,no000002,no000003 # kepco 1 + # no000001,no000002,no000003, # test 1 # , no000004 # jangys ,no000005 # whangsj @@ -343,7 +350,7 @@ class FaceDetect: # ,no000010,no000011,no000012,no000015,no000018 #Helmet on kepco2 ,no000019,no000020 #Helmet off kepco 2 ,no000021 #,no000022 - + ,no000100 # test ] result = [] @@ -352,7 +359,7 @@ class FaceDetect: result.append(i["encoding"]) names = [ - #'no000001','no000002','no000003'# kepco 1 + # 'no000001','no000002','no000003',# test 1 # , 'no000004' # jangys ,'no000005' # whangsj @@ -364,6 +371,7 @@ class FaceDetect: # ,'no000010','no000011','no000012','no000015','no000018' #Helmet on kepco2 ,'no000019','no000020' #Helmet off kepco 2 ,'no000021' #,no000022 + # ,'no000100' #test ] return result , names @@ -411,70 +419,62 @@ class FaceDetect: # face_encodings = [] # while self.mqtt_status != self.thread_value.STATUS_COMPLETE and self.stop_sign != True: - # while True: + while True: - # # TODO(JWKIM):ageing test 23-02-28 - # # self._mqtt_publish(status="AGEING_TEST") + # TODO(JWKIM):ageing test 23-02-28 + # self._mqtt_publish(status="AGEING_TEST") - # # loop stop - # if self.stop_event.is_set(): - # if self.mqtt_status == self.thread_value.STATUS_COMPLETE: - # pass - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # # TODO(JWKIM):ageing test 23-02-28 - # break + # loop stop + if self.stop_event.is_set(): + if self.mqtt_status == self.thread_value.STATUS_COMPLETE: + pass + else: + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 + break + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # TODO(JWKIM):ageing test 23-02-28 + break # Grab a single frame of video - ret, frame = input_movie.read() - - cv2.imwrite(AI_CONST.FTP_FR_RESULT,frame) - self._sftp_upload() - self.result = ["no000010"] + ret, frame = input_movie.read() - self._mqtt_publish(status = self.thread_value.STATUS_COMPLETE) - # Quit when the input video file ends # 영상파일일 경우 다시 재생 - # if not ret and os.path.isfile(self.input_video): - # input_movie = cv2.VideoCapture(self.input_video) - # continue + if not ret and os.path.isfile(self.input_video): + input_movie = cv2.VideoCapture(self.input_video) + continue # frame_count = int(input_movie.get(cv2.CV_CAP_PROP_FPS)) # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) - # rgb_frame = frame[:, :, ::-1] + rgb_frame = frame[:, :, ::-1] - # face_locations = face_recognition.face_locations(rgb_frame) - # face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) - # for face_encoding in face_encodings: + face_locations = face_recognition.face_locations(rgb_frame) + face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) + for face_encoding in face_encodings: - # matched_name = [] # detect 완료된 명단 + matched_name = [] # detect 완료된 명단 - # match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=AI_CONST.FACE_EVOLUTION_DISTANCE) + match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=AI_CONST.FACE_EVOLUTION_DISTANCE) - # #TODO(jwkim): 입력 이미지 변경 - # if self.fr_manager.fr_id_info != self.id_info: - # raise Exception(AI_CONST.IMG_CHANGED_MSG) + #TODO(jwkim): 입력 이미지 변경 + if self.fr_manager.fr_id_info != self.id_info: + raise Exception(AI_CONST.IMG_CHANGED_MSG) - # face_distances = face_recognition.face_distance(known_faces, face_encoding) - # best_match_index = np.argmin(face_distances) + face_distances = face_recognition.face_distance(known_faces, face_encoding) + best_match_index = np.argmin(face_distances) - # if face_distances[best_match_index] < AI_CONST.FACE_EVOLUTION_DISTANCE : - # #demo - # print(match) - # if match[best_match_index]: + if face_distances[best_match_index] < AI_CONST.FACE_EVOLUTION_DISTANCE : + #demo + if match[best_match_index]: - # # self.result[0] = known_names[best_match_index] - # self.result[0] = "no000010" - # self.mqtt_status = self.thread_value.STATUS_COMPLETE - # print(known_names[best_match_index]) - # self.stop_event.set() - # break + self.result[0] = "no000010" + print(known_names[best_match_index]) + self.mqtt_status = self.thread_value.STATUS_COMPLETE + self.stop_event.set() + break # if match[best_match_index]: # matched_name.append(self.worker_names[best_match_index]) @@ -486,52 +486,47 @@ class FaceDetect: # ri # self._ai_rbi(matched_name) - # if self.mqtt_status == self.thread_value.STATUS_NEW: - # self._mqtt_publish(self.thread_value.STATUS_NEW) + if self.mqtt_status == self.thread_value.STATUS_NEW: + self._mqtt_publish(self.thread_value.STATUS_NEW) - # # input source check - # self._source_check() + # input source check + self._source_check() - # # loop stop - # if self.stop_event.is_set(): - # if self.mqtt_status == self.thread_value.STATUS_COMPLETE: - # self.result_frame = copy.deepcopy(frame) - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # # TODO(JWKIM):ageing test 23-02-28 - # break + # loop stop + if self.stop_event.is_set(): - # if self.thread_value.timeout_status: - # self._mqtt_publish(self.thread_value.STATUS_TIMEOUT) - # self.thread_value.timeout_status = False + if self.mqtt_status == self.thread_value.STATUS_COMPLETE: + self.result_frame = copy.deepcopy(frame) + else: + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 + break + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # TODO(JWKIM):ageing test 23-02-28 + break - # elif self.stop_event.is_set(): - # if self.mqtt_status == self.thread_value.STATUS_COMPLETE: - # #sftp - - # cv2.imwrite(AI_CONST.FTP_FR_RESULT,self.result_frame) - - # if project_config.SFTP_UPLOAD and project_config.FR_UPLOAD: - # self.snapshot_path = self._sftp_upload() - # else: - # self.snapshot_path = None - - # self._mqtt_publish(self.thread_value.STATUS_COMPLETE) - # else: - # self._mqtt_publish(self.thread_value.STATUS_STOP) + if self.thread_value.timeout_status: + self._mqtt_publish(self.thread_value.STATUS_TIMEOUT) + self.thread_value.timeout_status = False - # if not self.timeout_event.is_set(): - # self.timeout_event.set() + elif self.stop_event.is_set(): + if self.mqtt_status == self.thread_value.STATUS_COMPLETE: + #sftp + cv2.imwrite(AI_CONST.FTP_FR_RESULT,self.result_frame) + self.snapshot_path = self._sftp_upload() + self._mqtt_publish(self.thread_value.STATUS_COMPLETE) + else: + self._mqtt_publish(self.thread_value.STATUS_STOP) - # self.timeout_event.clear() + if not self.timeout_event.is_set(): + self.timeout_event.set() + + self.timeout_event.clear() - # if self.mqtt_status == self.thread_value.STATUS_COMPLETE: + if self.mqtt_status == self.thread_value.STATUS_COMPLETE: - # pass + pass except Exception as e: print(e) diff --git a/DL/d2_od_detect.py b/DL/d2_od_detect.py index c9f0a22..ab7050f 100644 --- a/DL/d2_od_detect.py +++ b/DL/d2_od_detect.py @@ -81,7 +81,6 @@ from mqtt_publish import client import ai_engine_const as AI_CONST from REST_AI_ENGINE_CONTROL.app import models as M from custom_utils import LoadStreams -import project_config import AI_ENGINE.demo_utils as DEMO @@ -91,11 +90,7 @@ model = DetectMultiBackend(D2_ROOT / 'index_78.pt', device=device) stride, names, pt = model.stride, model.names, model.pt imgsz = check_img_size((640, 640), s=stride) # check image size -import paho.mqtt.client as mqtt -rtsp_client = mqtt.Client() -rtsp_client.username_pw_set("admin","admin") -rtsp_client.connect("localhost", 11883) -rtsp_client.publish("ptz", f"1,0.95,0", 0) + class CONDetect: """ @@ -201,271 +196,238 @@ class CONDetect: # Run inference self._publish(self.thread_value.STATUS_START, self.thread_value.result) - #while True: - # Dataloader - bs = 1 # batch_size - if webcam: - # LoadStreams 한번만 실행 - if not 'dataset' in locals(): - # view_img = check_imshow(warn=True) # imshow - dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride, - event=self.dataloader_event) - bs = len(dataset) - elif screenshot: - dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt) - else: - dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride) - vid_path, vid_writer = [None] * bs, [None] * bs + while True: + # Dataloader + bs = 1 # batch_size + if webcam: + # LoadStreams 한번만 실행 + if not 'dataset' in locals(): + # view_img = check_imshow(warn=True) # imshow + dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride, + event=self.dataloader_event) + bs = len(dataset) + elif screenshot: + dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt) + else: + dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride) + vid_path, vid_writer = [None] * bs, [None] * bs - model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup - seen, windows, dt = 0, [], (Profile(), Profile(), Profile()) + model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup + seen, windows, dt = 0, [], (Profile(), Profile(), Profile()) - for path, im, im0s, vid_cap, s in dataset: - # # loop stop - # if self.stop_event.is_set(): - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # pass - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # #TODO(JWKIM):ageing test 23-02-28 - # break + for path, im, im0s, vid_cap, s in dataset: + # loop stop + if self.stop_event.is_set(): + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + pass + else: + self.thread_value.current_status = self.thread_value.STATUS_STOP + #TODO(JWKIM):ageing test 23-02-28 + break - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # break #TODO(JWKIM):ageing test 23-02-28 + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # break #TODO(JWKIM):ageing test 23-02-28 - with dt[0]: - im = torch.from_numpy(im).to(model.device) - im = im.half() if model.fp16 else im.float() # uint8 to fp16/32 - im /= 255 # 0 - 255 to 0.0 - 1.0 - if len(im.shape) == 3: - im = im[None] # expand for batch dim + with dt[0]: + im = torch.from_numpy(im).to(model.device) + im = im.half() if model.fp16 else im.float() # uint8 to fp16/32 + im /= 255 # 0 - 255 to 0.0 - 1.0 + if len(im.shape) == 3: + im = im[None] # expand for batch dim - # Inference - with dt[1]: - visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False - pred = model(im, augment=augment, visualize=visualize) + # Inference + with dt[1]: + visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False + pred = model(im, augment=augment, visualize=visualize) - # NMS - with dt[2]: - pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det) + # NMS + with dt[2]: + pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det) - # Second-stage classifier (optional) - # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s) + # Second-stage classifier (optional) + # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s) - # Process predictions - for i, det in enumerate(pred): # per image - seen += 1 - if webcam: # batch_size >= 1 - p, im0, frame = path[i], im0s[i].copy(), dataset.count - s += f'{i}: ' + # Process predictions + for i, det in enumerate(pred): # per image + seen += 1 + if webcam: # batch_size >= 1 + p, im0, frame = path[i], im0s[i].copy(), dataset.count + s += f'{i}: ' + else: + p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0) + + p = Path(p) # to Path + save_path = str(save_dir / p.name) # im.jpg + txt_path = str(save_dir / 'labels' / p.stem) + ( + '' if dataset.mode == 'image' else f'_{frame}') # im.txt + s += '%gx%g ' % im.shape[2:] # print string + gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh + imc = im0.copy() if save_crop else im0 # for save_crop + annotator = Annotator(im0, line_width=line_thickness, example=str(names)) + + # FR START + #TODO(jwkim): PPE + FR + # imface = im0.copy() + # self._fr_inference(imface) + + # 딕셔너리 초기화 + detected_object = {} + + if len(det): + # Rescale boxes from img_size to im0 size + det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() + + # Print results + for c in det[:, 5].unique(): + n = (det[:, 5] == c).sum() # detections per class + s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string + + # Write results + for *xyxy, conf, cls in reversed(det): + if save_txt: # Write to file + xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view( + -1).tolist() # normalized xywh + line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format + with open(f'{txt_path}.txt', 'a') as f: + f.write(('%g ' * len(line)).rstrip() % line + '\n') + + if save_img or save_crop or view_img: # Add bbox to image + c = int(cls) # integer class + label = None if hide_labels else ( + names[c] if hide_conf else f'{names[c]} {conf:.2f}') + annotator.box_label(xyxy, label, color=colors(c, True)) + if save_crop: + save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', + BGR=True) + + c = int(cls) + + if names[c] in self.target_list: + # crop image 전송여부 + if self.model_info.con_model_info.crop_images: + # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False).tolist() + crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False) + crop_image_data = self._image_encoding(crop_image_data) + else: + crop_image_data = None + + if self.bbox_type == AI_CONST.BBOX_XYXY: + result_bbox = [int(xyxy[1]), int(xyxy[0]), int(xyxy[3]), int(xyxy[2])] + elif self.bbox_type == AI_CONST.BBOX_XYWH: + result_bbox = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() + + if names[c] != 'signalman': + detected_object[names[c]] = { + "name": names[c], + "cid": c, + "confidence": round(conf.item(), 2), + "bbox": result_bbox, + "image": crop_image_data # nparray -> list + } + + detected_object['signalman'] = { + "name": 'signalman', + "cid": 25, + "confidence": 0.7, + "bbox": [ 150, 150, 100, 200 ], + "image": None # nparray -> list + } + + self.result_frame = copy.deepcopy(im0) + + # input source check + self._source_check(dataset) + + # ri + detected_object = self._ai_rbi(detected_object) + + # update + self._result_update(detected_object) + + # publish new + if self.thread_value.current_status == self.thread_value.STATUS_NEW: + self._publish(self.thread_value.STATUS_NEW, self.thread_value.report_unit_result) + self.thread_value.current_status = self.thread_value.STATUS_NONE + + # publish complete + elif self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + cv2.imwrite(AI_CONST.FTP_CON_RESULT,self.result_frame) + self.snapshot_path = self._sftp_upload() + + self._publish(self.thread_value.STATUS_COMPLETE, self.thread_value.result) + self.stop_event.set() + + # if frame % 60 == 0 : + # #TODO(JWKIM):ageing test 23-02-28 + # self._publish("AGEING_TEST", self.thread_value.result) + + # #TODO(JWKIM):ageing test 23-02-28 + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + break + + # loop stop + if self.stop_event.is_set(): + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + pass + else: + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 + break + + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # #TODO(JWKIM):ageing test 23-02-28 + break + + # Stream results + im0 = annotator.result() + if view_img: + if platform.system() == 'Linux' and p not in windows: + windows.append(p) + cv2.namedWindow(str(p), + cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux) + cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0]) + # cv2.imshow(str(p), im0) # imshow + cv2.waitKey(1) # 1 millisecond + + # Save results (image with detections) + if save_img: + if dataset.mode == 'image': + cv2.imwrite(save_path, im0) + else: # 'video' or 'stream' + if vid_path[i] != save_path: # new video + vid_path[i] = save_path + if isinstance(vid_writer[i], cv2.VideoWriter): + vid_writer[i].release() # release previous video writer + if vid_cap: # video + fps = vid_cap.get(cv2.CAP_PROP_FPS) + w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + else: # stream + fps, w, h = 30, im0.shape[1], im0.shape[0] + save_path = str( + Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos + vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, + (w, h)) + vid_writer[i].write(im0) + + # # Print time (inference-only) + # LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms") + + # loop stop + if self.stop_event.is_set(): + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + pass else: - p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0) - - # p = Path(p) # to Path - # save_path = str(save_dir / p.name) # im.jpg - # txt_path = str(save_dir / 'labels' / p.stem) + ( - # '' if dataset.mode == 'image' else f'_{frame}') # im.txt - # s += '%gx%g ' % im.shape[2:] # print string - # gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh - # imc = im0.copy() if save_crop else im0 # for save_crop - # annotator = Annotator(im0, line_width=line_thickness, example=str(names)) - - self.thread_value.current_status == self.thread_value.STATUS_COMPLETE - cv2.imwrite(AI_CONST.FTP_CON_RESULT,copy.deepcopy(im0)) - if project_config.SFTP_UPLOAD: - self._sftp_upload() - demo_dict=[ - { - "name": 'signalman', - "cid": 1, - "confidence": 0.7, - "bbox": [ 150, 150, 100, 200 ], - "image": None # nparray -> list - }, - { - "name": 'sign_traffic_cone', - "cid": 25, - "confidence": 0.7, - "bbox": [ 150, 150, 100, 200 ], - "image": None # nparray -> list - }, - - - - - ] - print(demo_dict) - self._publish(status = self.thread_value.STATUS_COMPLETE, result = demo_dict) + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 break - break - # # FR START - # #TODO(jwkim): PPE + FR - # # imface = im0.copy() - # # self._fr_inference(imface) - - # # 딕셔너리 초기화 - # detected_object = {} - - # if len(det): - # # Rescale boxes from img_size to im0 size - # det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() - - # # Print results - # for c in det[:, 5].unique(): - # n = (det[:, 5] == c).sum() # detections per class - # s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string - - # # Write results - # for *xyxy, conf, cls in reversed(det): - # if save_txt: # Write to file - # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view( - # -1).tolist() # normalized xywh - # line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format - # with open(f'{txt_path}.txt', 'a') as f: - # f.write(('%g ' * len(line)).rstrip() % line + '\n') - - # if save_img or save_crop or view_img: # Add bbox to image - # c = int(cls) # integer class - # label = None if hide_labels else ( - # names[c] if hide_conf else f'{names[c]} {conf:.2f}') - # annotator.box_label(xyxy, label, color=colors(c, True)) - # if save_crop: - # save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', - # BGR=True) - - # c = int(cls) - - # # if names[c] in self.target_list: - # if names[c] == "sign_traffic_cone": - # # crop image 전송여부 - # if self.model_info.con_model_info.crop_images: - # # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False).tolist() - # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False) - # crop_image_data = self._image_encoding(crop_image_data) - # else: - # crop_image_data = None - - # if self.bbox_type == AI_CONST.BBOX_XYXY: - # result_bbox = [int(xyxy[1]), int(xyxy[0]), int(xyxy[3]), int(xyxy[2])] - # elif self.bbox_type == AI_CONST.BBOX_XYWH: - # result_bbox = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() - - # detected_object[names[c]] = { - # "name": names[c], - # "cid": c, - # "confidence": round(conf.item(), 2), - # "bbox": result_bbox, - # "image": crop_image_data # nparray -> list - # } - - # detected_object["signalman"] = { - # "name": "signalman", - # "cid": 0, - # "confidence": round(conf.item(), 2), - # "bbox": result_bbox, - # "image": None # nparray -> list - # } - - # self.result_frame = copy.deepcopy(im0) - - # # input source check - # self._source_check(dataset) - - # # ri - # detected_object = self._ai_rbi(detected_object) - - # # update - # self._result_update(detected_object) - - # # publish new - # if self.thread_value.current_status == self.thread_value.STATUS_NEW: - # self._publish(self.thread_value.STATUS_NEW, self.thread_value.report_unit_result) - # self.thread_value.current_status = self.thread_value.STATUS_NONE - - # # publish complete - # elif self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # cv2.imwrite(AI_CONST.FTP_CON_RESULT,self.result_frame) - - # if project_config.SFTP_UPLOAD: - # self.snapshot_path = self._sftp_upload() - # else: - # self.snapshot_path = None - - # self._publish(self.thread_value.STATUS_COMPLETE, self.thread_value.result) - # self.stop_event.set() - - # # if frame % 60 == 0 : - # # #TODO(JWKIM):ageing test 23-02-28 - # # self._publish("AGEING_TEST", self.thread_value.result) - - # # #TODO(JWKIM):ageing test 23-02-28 - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # break - - # # loop stop - # if self.stop_event.is_set(): - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # pass - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break - - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # # #TODO(JWKIM):ageing test 23-02-28 - # break - - # # Stream results - # im0 = annotator.result() - # if view_img: - # if platform.system() == 'Linux' and p not in windows: - # windows.append(p) - # cv2.namedWindow(str(p), - # cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux) - # cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0]) - # # cv2.imshow(str(p), im0) # imshow - # cv2.waitKey(1) # 1 millisecond - - # # Save results (image with detections) - # if save_img: - # if dataset.mode == 'image': - # cv2.imwrite(save_path, im0) - # else: # 'video' or 'stream' - # if vid_path[i] != save_path: # new video - # vid_path[i] = save_path - # if isinstance(vid_writer[i], cv2.VideoWriter): - # vid_writer[i].release() # release previous video writer - # if vid_cap: # video - # fps = vid_cap.get(cv2.CAP_PROP_FPS) - # w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) - # h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) - # else: # stream - # fps, w, h = 30, im0.shape[1], im0.shape[0] - # save_path = str( - # Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos - # vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, - # (w, h)) - # vid_writer[i].write(im0) - - # # # Print time (inference-only) - # # LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms") - - # # loop stop - # if self.stop_event.is_set(): - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # pass - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break - - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # # TODO(JWKIM):ageing test 23-02-28 - # break + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # TODO(JWKIM):ageing test 23-02-28 + break # # Print results # t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image @@ -476,15 +438,15 @@ class CONDetect: # if update: # strip_optimizer(weights[0]) # update model (to fix SourceChangeWarning) - # # timeout - # if self.thread_value.timeout_status: - # self._publish(self.thread_value.STATUS_TIMEOUT, self.thread_value.result) - # self.thread_value.timeout_status = False + # timeout + if self.thread_value.timeout_status: + self._publish(self.thread_value.STATUS_TIMEOUT, self.thread_value.result) + self.thread_value.timeout_status = False - # # stop - # elif self.stop_event.is_set() and \ - # self.thread_value.current_status != self.thread_value.STATUS_COMPLETE: - # self._publish(self.thread_value.STATUS_STOP, self.thread_value.result) + # stop + elif self.stop_event.is_set() and \ + self.thread_value.current_status != self.thread_value.STATUS_COMPLETE: + self._publish(self.thread_value.STATUS_STOP, self.thread_value.result) except Exception as e: self._publish(self.thread_value.STATUS_ERROR + str(e), self.thread_value.result) @@ -520,7 +482,7 @@ class CONDetect: remotepath = self.ftp_info.location + os.sep + self.ftp_info.file_con_setup + '.jpg' - #sftp.put(AI_CONST.FTP_CON_RESULT, remotepath) + sftp.put(AI_CONST.FTP_CON_RESULT, remotepath) sftp.close() transprot.close() @@ -739,277 +701,229 @@ class PPEDetect: # Run inference self._publish(self.thread_value.STATUS_START, self.thread_value.result) - # while True: - # Dataloader - bs = 1 # batch_size - if webcam: - # LoadStreams 한번만 실행 - if not 'dataset' in locals(): - # view_img = check_imshow(warn=True) # imshow - dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride, - event=self.dataloader_event) - bs = len(dataset) - elif screenshot: - dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt) - else: - dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride) - vid_path, vid_writer = [None] * bs, [None] * bs + while True: + # Dataloader + bs = 1 # batch_size + if webcam: + # LoadStreams 한번만 실행 + if not 'dataset' in locals(): + # view_img = check_imshow(warn=True) # imshow + dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride, + event=self.dataloader_event) + bs = len(dataset) + elif screenshot: + dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt) + else: + dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride) + vid_path, vid_writer = [None] * bs, [None] * bs - model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup - seen, windows, dt = 0, [], (Profile(), Profile(), Profile()) + model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup + seen, windows, dt = 0, [], (Profile(), Profile(), Profile()) - for path, im, im0s, vid_cap, s in dataset: - - # # loop stop - # if self.stop_event.is_set(): - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # pass - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # #TODO(JWKIM):ageing test 23-02-28 - # break + for path, im, im0s, vid_cap, s in dataset: + # loop stop + if self.stop_event.is_set(): + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + pass + else: + self.thread_value.current_status = self.thread_value.STATUS_STOP + #TODO(JWKIM):ageing test 23-02-28 + break - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # break #TODO(JWKIM):ageing test 23-02-28 + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # break #TODO(JWKIM):ageing test 23-02-28 - with dt[0]: - im = torch.from_numpy(im).to(model.device) - im = im.half() if model.fp16 else im.float() # uint8 to fp16/32 - im /= 255 # 0 - 255 to 0.0 - 1.0 - if len(im.shape) == 3: - im = im[None] # expand for batch dim + with dt[0]: + im = torch.from_numpy(im).to(model.device) + im = im.half() if model.fp16 else im.float() # uint8 to fp16/32 + im /= 255 # 0 - 255 to 0.0 - 1.0 + if len(im.shape) == 3: + im = im[None] # expand for batch dim - # Inference - with dt[1]: - visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False - pred = model(im, augment=augment, visualize=visualize) + # Inference + with dt[1]: + visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False + pred = model(im, augment=augment, visualize=visualize) - # NMS - with dt[2]: - pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det) + # NMS + with dt[2]: + pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det) - # Second-stage classifier (optional) - # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s) + # Second-stage classifier (optional) + # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s) - # Process predictions - for i, det in enumerate(pred): # per image - seen += 1 - if webcam: # batch_size >= 1 - p, im0, frame = path[i], im0s[i].copy(), dataset.count - s += f'{i}: ' - else: - p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0) + # Process predictions + for i, det in enumerate(pred): # per image + seen += 1 + if webcam: # batch_size >= 1 + p, im0, frame = path[i], im0s[i].copy(), dataset.count + s += f'{i}: ' + else: + p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0) - # p = Path(p) # to Path - # save_path = str(save_dir / p.name) # im.jpg - # txt_path = str(save_dir / 'labels' / p.stem) + ( - # '' if dataset.mode == 'image' else f'_{frame}') # im.txt - # s += '%gx%g ' % im.shape[2:] # print string - # gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh - # imc = im0.copy() if save_crop else im0 # for save_crop - # annotator = Annotator(im0, line_width=line_thickness, example=str(names)) + p = Path(p) # to Path + save_path = str(save_dir / p.name) # im.jpg + txt_path = str(save_dir / 'labels' / p.stem) + ( + '' if dataset.mode == 'image' else f'_{frame}') # im.txt + s += '%gx%g ' % im.shape[2:] # print string + gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh + imc = im0.copy() if save_crop else im0 # for save_crop + annotator = Annotator(im0, line_width=line_thickness, example=str(names)) - self.thread_value.current_status == self.thread_value.STATUS_COMPLETE - cv2.imwrite(AI_CONST.FTP_PPE_RESULT,copy.deepcopy(im0)) - if project_config.SFTP_UPLOAD: - self._sftp_upload() - demo_dict=[ - { - "name": 'safety_helmet_on', - "cid": 1, - "confidence": 0.7, - "bbox": [ 150, 150, 100, 200 ], - "image": None # nparray -> list - }, - { - "name": 'safety_gloves_work_on', - "cid": 25, - "confidence": 0.7, - "bbox": [ 150, 150, 100, 200 ], - "image": None # nparray -> list - }, - { - "name": 'safety_boots_on', - "cid": 25, - "confidence": 0.7, - "bbox": [ 150, 150, 100, 200 ], - "image": None # nparray -> list - } - ] - print(demo_dict) - self._publish(status = self.thread_value.STATUS_COMPLETE, result = demo_dict) - break - break - - # # FR START - # #TODO(jwkim): PPE + FR - # # imface = im0.copy() - # # self._fr_inference(imface) + # FR START + #TODO(jwkim): PPE + FR + # imface = im0.copy() + # self._fr_inference(imface) - # # 딕셔너리 초기화 - # detected_object = {} + # 딕셔너리 초기화 + detected_object = {} - # if len(det): - # # Rescale boxes from img_size to im0 size - # det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() + if len(det): + # Rescale boxes from img_size to im0 size + det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() - # # Print results - # for c in det[:, 5].unique(): - # n = (det[:, 5] == c).sum() # detections per class - # s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string + # Print results + for c in det[:, 5].unique(): + n = (det[:, 5] == c).sum() # detections per class + s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string - # # Write results - # for *xyxy, conf, cls in reversed(det): - # if save_txt: # Write to file - # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view( - # -1).tolist() # normalized xywh - # line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format - # with open(f'{txt_path}.txt', 'a') as f: - # f.write(('%g ' * len(line)).rstrip() % line + '\n') + # Write results + for *xyxy, conf, cls in reversed(det): + if save_txt: # Write to file + xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view( + -1).tolist() # normalized xywh + line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format + with open(f'{txt_path}.txt', 'a') as f: + f.write(('%g ' * len(line)).rstrip() % line + '\n') - # if save_img or save_crop or view_img: # Add bbox to image - # c = int(cls) # integer class - # label = None if hide_labels else ( - # names[c] if hide_conf else f'{names[c]} {conf:.2f}') - # annotator.box_label(xyxy, label, color=colors(c, True)) - # if save_crop: - # save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', - # BGR=True) + if save_img or save_crop or view_img: # Add bbox to image + c = int(cls) # integer class + label = None if hide_labels else ( + names[c] if hide_conf else f'{names[c]} {conf:.2f}') + annotator.box_label(xyxy, label, color=colors(c, True)) + if save_crop: + save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', + BGR=True) - # c = int(cls) + c = int(cls) - # if names[c] in self.target_list: - # # crop image 전송여부 - # if self.model_info.ppe_model_info.crop_images: - # # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False).tolist() - # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False) - # crop_image_data = self._image_encoding(crop_image_data) - # else: - # crop_image_data = None + if names[c] in self.target_list: + # crop image 전송여부 + if self.model_info.ppe_model_info.crop_images: + # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False).tolist() + crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False) + crop_image_data = self._image_encoding(crop_image_data) + else: + crop_image_data = None - # if self.bbox_type == AI_CONST.BBOX_XYXY: - # result_bbox = [int(xyxy[1]), int(xyxy[0]), int(xyxy[3]), int(xyxy[2])] - # elif self.bbox_type == AI_CONST.BBOX_XYWH: - # result_bbox = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() + if self.bbox_type == AI_CONST.BBOX_XYXY: + result_bbox = [int(xyxy[1]), int(xyxy[0]), int(xyxy[3]), int(xyxy[2])] + elif self.bbox_type == AI_CONST.BBOX_XYWH: + result_bbox = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() - # if [names[c]] == "safety_gloves_work_on": - # pass - - # detected_object["safety_gloves_work_on"] = { - # "name": "safety_gloves_work_on", - # "cid": 5, - # "confidence": round(conf.item(), 2), - # "bbox": result_bbox, - # "image": None # nparray -> list - # } - - # detected_object[names[c]] = { - # "name": names[c], - # "cid": c, - # "confidence": round(conf.item(), 2), - # "bbox": result_bbox, - # "image": crop_image_data # nparray -> list - # } + detected_object[names[c]] = { + "name": names[c], + "cid": c, + "confidence": round(conf.item(), 2), + "bbox": result_bbox, + "image": crop_image_data # nparray -> list + } - # self.result_frame = copy.deepcopy(im0) + self.result_frame = copy.deepcopy(im0) - # # input source check - # self._source_check(dataset) + # input source check + self._source_check(dataset) - # # ri - # detected_object = self._ai_rbi(detected_object) + # ri + detected_object = self._ai_rbi(detected_object) - # # update - # self._result_update(detected_object) + # update + self._result_update(detected_object) - # # publish new - # if self.thread_value.current_status == self.thread_value.STATUS_NEW: - # self._publish(self.thread_value.STATUS_NEW, self.thread_value.report_unit_result) - # self.thread_value.current_status = self.thread_value.STATUS_NONE + # publish new + if self.thread_value.current_status == self.thread_value.STATUS_NEW: + self._publish(self.thread_value.STATUS_NEW, self.thread_value.report_unit_result) + self.thread_value.current_status = self.thread_value.STATUS_NONE - # # publish complete - # elif self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # cv2.imwrite(AI_CONST.FTP_PPE_RESULT,self.result_frame) + # publish complete + elif self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + cv2.imwrite(AI_CONST.FTP_PPE_RESULT,self.result_frame) + self.snapshot_path = self._sftp_upload() - # if project_config.SFTP_UPLOAD: - # self.snapshot_path = self._sftp_upload() - # else: - # self.snapshot_path = None - - # self._publish(self.thread_value.STATUS_COMPLETE, self.thread_value.result) - # self.stop_event.set() + self._publish(self.thread_value.STATUS_COMPLETE, self.thread_value.result) + self.stop_event.set() - # # if frame % 60 == 0 : - # # #TODO(JWKIM):ageing test 23-02-28 - # # self._publish("AGEING_TEST", self.thread_value.result) + # if frame % 60 == 0 : + # #TODO(JWKIM):ageing test 23-02-28 + # self._publish("AGEING_TEST", self.thread_value.result) - # # #TODO(JWKIM):ageing test 23-02-28 - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # break + # #TODO(JWKIM):ageing test 23-02-28 + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + break - # # loop stop - # if self.stop_event.is_set(): - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # pass - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break + # loop stop + if self.stop_event.is_set(): + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + pass + else: + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 + break - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # # #TODO(JWKIM):ageing test 23-02-28 - # break + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # #TODO(JWKIM):ageing test 23-02-28 + break - # # Stream results - # im0 = annotator.result() - # if view_img: - # if platform.system() == 'Linux' and p not in windows: - # windows.append(p) - # cv2.namedWindow(str(p), - # cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux) - # cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0]) - # # cv2.imshow(str(p), im0) # imshow - # cv2.waitKey(1) # 1 millisecond + # Stream results + im0 = annotator.result() + if view_img: + if platform.system() == 'Linux' and p not in windows: + windows.append(p) + cv2.namedWindow(str(p), + cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux) + cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0]) + # cv2.imshow(str(p), im0) # imshow + cv2.waitKey(1) # 1 millisecond - # # Save results (image with detections) - # if save_img: - # if dataset.mode == 'image': - # cv2.imwrite(save_path, im0) - # else: # 'video' or 'stream' - # if vid_path[i] != save_path: # new video - # vid_path[i] = save_path - # if isinstance(vid_writer[i], cv2.VideoWriter): - # vid_writer[i].release() # release previous video writer - # if vid_cap: # video - # fps = vid_cap.get(cv2.CAP_PROP_FPS) - # w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) - # h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) - # else: # stream - # fps, w, h = 30, im0.shape[1], im0.shape[0] - # save_path = str( - # Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos - # vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, - # (w, h)) - # vid_writer[i].write(im0) + # Save results (image with detections) + if save_img: + if dataset.mode == 'image': + cv2.imwrite(save_path, im0) + else: # 'video' or 'stream' + if vid_path[i] != save_path: # new video + vid_path[i] = save_path + if isinstance(vid_writer[i], cv2.VideoWriter): + vid_writer[i].release() # release previous video writer + if vid_cap: # video + fps = vid_cap.get(cv2.CAP_PROP_FPS) + w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + else: # stream + fps, w, h = 30, im0.shape[1], im0.shape[0] + save_path = str( + Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos + vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, + (w, h)) + vid_writer[i].write(im0) # # Print time (inference-only) # LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms") - # # loop stop - # if self.stop_event.is_set(): - # if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: - # pass - # else: - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break + # loop stop + if self.stop_event.is_set(): + if self.thread_value.current_status == self.thread_value.STATUS_COMPLETE: + pass + else: + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 + break - # elif self.thread_value.timeout_status: - # self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT - # # TODO(JWKIM):ageing test 23-02-28 - # break + elif self.thread_value.timeout_status: + self.thread_value.current_status = self.thread_value.STATUS_TIMEOUT + # TODO(JWKIM):ageing test 23-02-28 + break # # Print results # t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image @@ -1020,15 +934,15 @@ class PPEDetect: # if update: # strip_optimizer(weights[0]) # update model (to fix SourceChangeWarning) - # # timeout - # if self.thread_value.timeout_status: - # self._publish(self.thread_value.STATUS_TIMEOUT, self.thread_value.result) - # self.thread_value.timeout_status = False + # timeout + if self.thread_value.timeout_status: + self._publish(self.thread_value.STATUS_TIMEOUT, self.thread_value.result) + self.thread_value.timeout_status = False - # # stop - # elif self.stop_event.is_set() and \ - # self.thread_value.current_status != self.thread_value.STATUS_COMPLETE: - # self._publish(self.thread_value.STATUS_STOP, self.thread_value.result) + # stop + elif self.stop_event.is_set() and \ + self.thread_value.current_status != self.thread_value.STATUS_COMPLETE: + self._publish(self.thread_value.STATUS_STOP, self.thread_value.result) except Exception as e: self._publish(self.thread_value.STATUS_ERROR + str(e), self.thread_value.result) @@ -1064,7 +978,7 @@ class PPEDetect: remotepath = self.ftp_info.location + os.sep + self.ftp_info.file_ppe + '.jpg' - #sftp.put(AI_CONST.FTP_PPE_RESULT, remotepath) + sftp.put(AI_CONST.FTP_PPE_RESULT, remotepath) sftp.close() transprot.close() @@ -1103,6 +1017,7 @@ class PPEDetect: matched_name.append(self.worker_names[best_match_index]) print(self.worker_names[best_match_index]) + def _fr_encoding_list(self): """ 안면인식 대상 이미지 등록 @@ -1147,7 +1062,7 @@ class PPEDetect: """ if detected: self.thread_value.report_unit_result = [] - self.thread_value.report_unit_result = [None] * len(list(dict(self.targets).keys())) + self.thread_value.report_unit_result = [None] * len(self.target_list) for key, value in detected.items(): if self.thread_value.result[self.target_list.index(key)] is None: @@ -1178,8 +1093,7 @@ class PPEDetect: } if self.snapshot_path: result_message[AI_CONST.DEMO_KEY_NAME_SNAPSHOT_SFTP] = self.snapshot_path - - print(result_message) + client.publish(AI_CONST.MQTT_PPE_TOPIC, json.dumps(result_message), 0) # client.loop_stop() @@ -1317,11 +1231,11 @@ class WDDetect: if os.path.exists(AI_CONST.FTP_WD_RESULT): os.remove(AI_CONST.FTP_WD_RESULT) - # if os.path.exists(AI_CONST.FTP_BI_RESULT): - # os.remove(AI_CONST.FTP_BI_RESULT) + if os.path.exists(AI_CONST.FTP_BI_RESULT): + os.remove(AI_CONST.FTP_BI_RESULT) - # #demo - # DEMO.demo_wd_bi(1) + #demo + DEMO.demo_wd_bi(1) source = str(source) save_img = not nosave and not source.endswith('.txt') # save inference images @@ -1343,220 +1257,200 @@ class WDDetect: # imgsz = check_img_size(imgsz, s=stride) # check image size # Run inference - self._publish(topic = AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_START,raise_list = []) + self._publish(topic = AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_START) - #while True: - # Dataloader - bs = 1 # batch_size - if webcam: - # stream 영상은 한번만 load - if not 'dataset' in locals(): - # view_img = check_imshow(warn=True) # imshow - dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride, - event=self.dataloader_event) - bs = len(dataset) - elif screenshot: - dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt) - else: - dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride) - vid_path, vid_writer = [None] * bs, [None] * bs + while True: + # Dataloader + bs = 1 # batch_size + if webcam: + # stream 영상은 한번만 load + if not 'dataset' in locals(): + # view_img = check_imshow(warn=True) # imshow + dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride, + event=self.dataloader_event) + bs = len(dataset) + elif screenshot: + dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt) + else: + dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride) + vid_path, vid_writer = [None] * bs, [None] * bs - model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup - seen, windows, dt = 0, [], (Profile(), Profile(), Profile()) - for path, im, im0s, vid_cap, s in dataset: + model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup + seen, windows, dt = 0, [], (Profile(), Profile(), Profile()) + for path, im, im0s, vid_cap, s in dataset: - # # loop stop - # if self.stop_event.is_set(): - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # #TODO(JWKIM):ageing test 23-02-28 - # break + # loop stop + if self.stop_event.is_set(): + self.thread_value.current_status = self.thread_value.STATUS_STOP + #TODO(JWKIM):ageing test 23-02-28 + break - with dt[0]: - im = torch.from_numpy(im).to(model.device) - im = im.half() if model.fp16 else im.float() # uint8 to fp16/32 - im /= 255 # 0 - 255 to 0.0 - 1.0 - if len(im.shape) == 3: - im = im[None] # expand for batch dim + with dt[0]: + im = torch.from_numpy(im).to(model.device) + im = im.half() if model.fp16 else im.float() # uint8 to fp16/32 + im /= 255 # 0 - 255 to 0.0 - 1.0 + if len(im.shape) == 3: + im = im[None] # expand for batch dim - # Inference - with dt[1]: - visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False - pred = model(im, augment=augment, visualize=visualize) + # Inference + with dt[1]: + visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False + pred = model(im, augment=augment, visualize=visualize) - # NMS - with dt[2]: - pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det) + # NMS + with dt[2]: + pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det) - # Second-stage classifier (optional) - # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s) + # Second-stage classifier (optional) + # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s) - # Process predictions - for i, det in enumerate(pred): # per image - seen += 1 - if webcam: # batch_size >= 1 - p, im0, frame = path[i], im0s[i].copy(), dataset.count - s += f'{i}: ' - else: - p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0) + # Process predictions + for i, det in enumerate(pred): # per image + seen += 1 + if webcam: # batch_size >= 1 + p, im0, frame = path[i], im0s[i].copy(), dataset.count + s += f'{i}: ' + else: + p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0) - # p = Path(p) # to Path - # save_path = str(save_dir / p.name) # im.jpg - # txt_path = str(save_dir / 'labels' / p.stem) + ( - # '' if dataset.mode == 'image' else f'_{frame}') # im.txt - # s += '%gx%g ' % im.shape[2:] # print string - # gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh - # imc = im0.copy() if save_crop else im0 # for save_crop - # annotator = Annotator(im0, line_width=line_thickness, example=str(names)) + p = Path(p) # to Path + save_path = str(save_dir / p.name) # im.jpg + txt_path = str(save_dir / 'labels' / p.stem) + ( + '' if dataset.mode == 'image' else f'_{frame}') # im.txt + s += '%gx%g ' % im.shape[2:] # print string + gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh + imc = im0.copy() if save_crop else im0 # for save_crop + annotator = Annotator(im0, line_width=line_thickness, example=str(names)) - self.thread_value.current_status == self.thread_value.STATUS_DETECT - cv2.imwrite(AI_CONST.FTP_WD_RESULT,copy.deepcopy(im0)) - # if project_config.SFTP_UPLOAD: - self._sftp_upload(topic = AI_CONST.MQTT_WD_TOPIC) - demo_dict=[ - { - "name": 'safety_helmet_off', - "cid": 2, - "confidence": 0.7, - "bbox": [ 150, 150, 100, 200 ], - "image": None # nparray -> list - } - ] - print(demo_dict) - self._publish(topic=AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_DETECT, raise_list = demo_dict) + # detect list + self.detect_list = [] + + if len(det): + # Rescale boxes from img_size to im0 size + det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() + + # Print results + for c in det[:, 5].unique(): + n = (det[:, 5] == c).sum() # detections per class + s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string + + # Write results + for *xyxy, conf, cls in reversed(det): + if save_txt: # Write to file + xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view( + -1).tolist() # normalized xywh + line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format + with open(f'{txt_path}.txt', 'a') as f: + f.write(('%g ' * len(line)).rstrip() % line + '\n') + + if save_img or save_crop or view_img: # Add bbox to image + c = int(cls) # integer class + label = None if hide_labels else ( + names[c] if hide_conf else f'{names[c]} {conf:.2f}') + annotator.box_label(xyxy, label, color=colors(c, True)) + if save_crop: + save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', + BGR=True) + + # jwkim + c = int(cls) + + # crop image 전송여부 + if self.model_info.wd_model_info.crop_images: + # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False).tolist() + crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False) + crop_image_data = self._image_encoding(crop_image_data) + else: + crop_image_data = None + + if self.bbox_type == AI_CONST.BBOX_XYXY: + result_bbox = [int(xyxy[1]), int(xyxy[0]), int(xyxy[3]), int(xyxy[2])] + elif self.bbox_type == AI_CONST.BBOX_XYWH: + result_bbox = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() + + detected_object = { + "name": names[c], + "cid": c, + "confidence": round(conf.item(), 2), + "bbox": result_bbox, + "image": crop_image_data # nparray -> list + } + if detected_object["cid"] in AI_CONST.OFF_CLASS_LIST: + # off class 만 저장 + self.detect_list.append(detected_object) + + self.result_frame = copy.deepcopy(im0) + + # input source check + self._source_check(dataset) + + # ri update + self._ai_rbi(dataset, self.detect_list) + + #demo + if DEMO.DEMO_WD_BI_CONST == 2: + if not os.path.exists(AI_CONST.FTP_BI_RESULT): + cv2.imwrite(AI_CONST.FTP_BI_RESULT,self.result_frame) + self.snapshot_path_bi = self._sftp_upload(topic = AI_CONST.MQTT_BI_TOPIC) + self._publish_bi_demo(topic = AI_CONST.MQTT_BI_TOPIC, status = self.thread_value.STATUS_DETECT) + self.snapshot_path_bi = None + + if self.detect_list and self.current_ri > self.ri_info.work_define_ri : + self.thread_value.current_status = self.thread_value.STATUS_DETECT + + if not os.path.exists(AI_CONST.FTP_WD_RESULT): + cv2.imwrite(AI_CONST.FTP_WD_RESULT,self.result_frame) + self.snapshot_path = self._sftp_upload(topic = AI_CONST.MQTT_WD_TOPIC) + self._publish(topic = AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_DETECT) + + self.snapshot_path = None + + # loop stop + if self.stop_event.is_set(): + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 + break + + # Stream results + im0 = annotator.result() + if view_img: + if platform.system() == 'Linux' and p not in windows: + windows.append(p) + cv2.namedWindow(str(p), + cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux) + cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0]) + # cv2.imshow(str(p), im0) # imshow + cv2.waitKey(1) # 1 millisecond + + # Save results (image with detections) + if save_img: + if dataset.mode == 'image': + cv2.imwrite(save_path, im0) + else: # 'video' or 'stream' + if vid_path[i] != save_path: # new video + vid_path[i] = save_path + if isinstance(vid_writer[i], cv2.VideoWriter): + vid_writer[i].release() # release previous video writer + if vid_cap: # video + fps = vid_cap.get(cv2.CAP_PROP_FPS) + w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) + h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + else: # stream + fps, w, h = 30, im0.shape[1], im0.shape[0] + save_path = str( + Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos + vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, + (w, h)) + vid_writer[i].write(im0) + + # Print time (inference-only) + # LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms") + + # loop stop + if self.stop_event.is_set(): + self.thread_value.current_status = self.thread_value.STATUS_STOP + # TODO(JWKIM):ageing test 23-02-28 break - break - - # # detect list - # self.detect_list = [] - - # if len(det): - # # Rescale boxes from img_size to im0 size - # det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() - - # # Print results - # for c in det[:, 5].unique(): - # n = (det[:, 5] == c).sum() # detections per class - # s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string - - # # Write results - # for *xyxy, conf, cls in reversed(det): - # if save_txt: # Write to file - # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view( - # -1).tolist() # normalized xywh - # line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format - # with open(f'{txt_path}.txt', 'a') as f: - # f.write(('%g ' * len(line)).rstrip() % line + '\n') - - # if save_img or save_crop or view_img: # Add bbox to image - # c = int(cls) # integer class - # label = None if hide_labels else ( - # names[c] if hide_conf else f'{names[c]} {conf:.2f}') - # annotator.box_label(xyxy, label, color=colors(c, True)) - # if save_crop: - # save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', - # BGR=True) - - # # jwkim - # c = int(cls) - - # # crop image 전송여부 - # if self.model_info.wd_model_info.crop_images: - # # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False).tolist() - # crop_image_data = save_one_box(xyxy, imc, BGR=True, save=False) - # crop_image_data = self._image_encoding(crop_image_data) - # else: - # crop_image_data = None - - # if self.bbox_type == AI_CONST.BBOX_XYXY: - # result_bbox = [int(xyxy[1]), int(xyxy[0]), int(xyxy[3]), int(xyxy[2])] - # elif self.bbox_type == AI_CONST.BBOX_XYWH: - # result_bbox = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() - - # detected_object = { - # "name": names[c], - # "cid": c, - # "confidence": round(conf.item(), 2), - # "bbox": result_bbox, - # "image": crop_image_data # nparray -> list - # } - # if detected_object["cid"] in AI_CONST.OFF_CLASS_LIST: - # # off class 만 저장 - # self.detect_list.append(detected_object) - - # self.result_frame = copy.deepcopy(im0) - - # # input source check - # self._source_check(dataset) - - # # ri update - # self._ai_rbi(dataset, self.detect_list) - - # #demo - # if DEMO.DEMO_WD_BI_CONST == 2: - # if not os.path.exists(AI_CONST.FTP_BI_RESULT): - # cv2.imwrite(AI_CONST.FTP_BI_RESULT,self.result_frame) - # if project_config.UPLOAD: - # self.snapshot_path_bi = self._sftp_upload(topic = AI_CONST.MQTT_BI_TOPIC) - # self._publish_bi_demo(topic = AI_CONST.MQTT_BI_TOPIC, status = self.thread_value.STATUS_DETECT) - # self.snapshot_path_bi = None - - # if self.detect_list and self.current_ri > self.ri_info.work_define_ri : - # self.thread_value.current_status = self.thread_value.STATUS_DETECT - - # if not os.path.exists(AI_CONST.FTP_WD_RESULT): - # cv2.imwrite(AI_CONST.FTP_WD_RESULT,self.result_frame) - # if project_config.UPLOAD: - # self.snapshot_path = self._sftp_upload(topic = AI_CONST.MQTT_WD_TOPIC) - # self._publish(topic = AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_DETECT) - - # self.snapshot_path = None - - # # loop stop - # if self.stop_event.is_set(): - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break - - # # Stream results - # im0 = annotator.result() - # if view_img: - # if platform.system() == 'Linux' and p not in windows: - # windows.append(p) - # cv2.namedWindow(str(p), - # cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux) - # cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0]) - # # cv2.imshow(str(p), im0) # imshow - # cv2.waitKey(1) # 1 millisecond - - # # Save results (image with detections) - # if save_img: - # if dataset.mode == 'image': - # cv2.imwrite(save_path, im0) - # else: # 'video' or 'stream' - # if vid_path[i] != save_path: # new video - # vid_path[i] = save_path - # if isinstance(vid_writer[i], cv2.VideoWriter): - # vid_writer[i].release() # release previous video writer - # if vid_cap: # video - # fps = vid_cap.get(cv2.CAP_PROP_FPS) - # w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) - # h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) - # else: # stream - # fps, w, h = 30, im0.shape[1], im0.shape[0] - # save_path = str( - # Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos - # vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, - # (w, h)) - # vid_writer[i].write(im0) - - # # Print time (inference-only) - # # LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms") - - # # loop stop - # if self.stop_event.is_set(): - # self.thread_value.current_status = self.thread_value.STATUS_STOP - # # TODO(JWKIM):ageing test 23-02-28 - # break # # Print results # t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image @@ -1567,8 +1461,8 @@ class WDDetect: # if update: # strip_optimizer(weights[0]) # update model (to fix SourceChangeWarning) - # if self.stop_event.is_set(): - # self._publish(topic = AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_STOP) + if self.stop_event.is_set(): + self._publish(topic = AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_STOP) except Exception as e: self._publish(topic = AI_CONST.MQTT_WD_TOPIC, status = self.thread_value.STATUS_ERROR + str(e), raise_list=self.detect_list) @@ -1602,13 +1496,12 @@ class WDDetect: sftp = paramiko.SFTPClient.from_transport(transprot) if topic == AI_CONST.MQTT_WD_TOPIC: - print('wd') remotepath = self.ftp_info.location + os.sep + self.ftp_info.file_wd + '.jpg' - #sftp.put(AI_CONST.FTP_WD_RESULT, remotepath) + sftp.put(AI_CONST.FTP_WD_RESULT, remotepath) elif topic == AI_CONST.MQTT_BI_TOPIC: remotepath = self.ftp_info.location + os.sep + self.ftp_info.file_bi + '.jpg' - #sftp.put(AI_CONST.FTP_BI_RESULT, remotepath) + sftp.put(AI_CONST.FTP_BI_RESULT, remotepath) sftp.close() transprot.close() @@ -1657,11 +1550,11 @@ class WDDetect: "datetime": self.thread_value.date_utill.date_str_micro_sec(), "status": status, "result": { - 'construction_type': 'D4', - 'procedure_no': 1, - 'procedure_ri': 0.7, - 'ri': 0.7, - 'detect_list': raise_list + 'construction_type': self.ri_info.construction_code, + 'procedure_no': self.ri_info.work_no, + 'procedure_ri': self.ri_info.work_define_ri, + 'ri': self.current_ri, + 'detect_list': raise_list if raise_list else self.detect_list } } diff --git a/DL/wd.streams b/DL/wd.streams index 11e64b8..f56ee22 100644 --- a/DL/wd.streams +++ b/DL/wd.streams @@ -1 +1 @@ -rtsp://admin:admin1263!@10.20.10.99:28554/onvif/media?profile=Profile2 +rtsp://admin:admin1263!@10.20.10.99:554/onvif/media?profile=Profile2 diff --git a/REST_AI_ENGINE_CONTROL/app/routes/services.py b/REST_AI_ENGINE_CONTROL/app/routes/services.py index b1c8a70..038594b 100644 --- a/REST_AI_ENGINE_CONTROL/app/routes/services.py +++ b/REST_AI_ENGINE_CONTROL/app/routes/services.py @@ -18,7 +18,6 @@ from sqlalchemy.orm import Session from starlette.requests import Request import copy -import time import threading AI_ENGINE_PATH = "/AI_ENGINE" @@ -289,7 +288,6 @@ async def ai_engine_od_con_setup_detect(request: Request, request_body_info: M.A } ``` """ - # time.sleep(2) global engine_info message_queue.sender({ @@ -592,7 +590,6 @@ async def ai_engine_od_ppe_detect(request: Request, request_body_info: M.AEAIMod } ``` """ - # time.sleep(2) global engine_info message_queue.sender({ @@ -768,7 +765,6 @@ async def ai_engine_od_work_detect(request: Request, request_body_info: M.AEAIMo } ``` """ - global engine_info message_queue.sender({ @@ -854,7 +850,7 @@ async def ai_engine_bi_detect(request: Request, request_body_info: M.AEAIModelRB # DEMO.demo_wd_bi(2) - # DEMO.bi_snap_shot() + DEMO.bi_snap_shot() result = M.ResponseBase() diff --git a/ai_engine_const.py b/ai_engine_const.py index 8604f96..6eb7f13 100644 --- a/ai_engine_const.py +++ b/ai_engine_const.py @@ -52,24 +52,24 @@ elif project_config.CONFIG == project_config.CONFIG_MG: # MQTT_USER_ID = 'admin' # MQTT_USER_PW = 'admin' - MQTT_PORT = 11883 + MQTT_PORT = 1883 MQTT_USER_ID = 'kepco' MQTT_USER_PW = '!kepco1234' # #source # RTSP = "rtsp://10.20.10.1:8554/cam/0/low" # RTSP = "rtsp://192.168.39.20:8554/cam/0/low" - RTSP = "rtsp://admin:admin1263!@10.20.10.99:28554/onvif/media?profile=Profile2" + RTSP = "rtsp://admin:admin1263!@10.20.10.99:554/onvif/media?profile=Profile2" CON_SOURCE = RTSP FR_SOURCE = RTSP PPE_SOURCE = RTSP WD_SOURCE = RTSP # ----------- ---------- - #CON_SOURCE = "rtsp://219.250.188.204:8554/con" - #FR_SOURCE = "rtsp://219.250.188.205:8554/fr" - #PPE_SOURCE = "rtsp://219.250.188.206:8554/ppe" - #WD_SOURCE = "rtsp://219.250.188.207:8554/wd" + # CON_SOURCE = "rtsp://219.250.188.204:8554/con" + # FR_SOURCE = "rtsp://219.250.188.205:8554/fr" + # PPE_SOURCE = "rtsp://219.250.188.206:8554/ppe" + # WD_SOURCE = "rtsp://219.250.188.207:8554/wd" # ----------- ---------- if project_config.DEBUG_MODE: @@ -162,6 +162,10 @@ WORKER1_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/kepco1.jpg" WORKER2_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/kepco1_1.jpg" WORKER3_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/kepco1_2.jpg" +WORKER1_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/facerec_worker1.png" +WORKER2_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/facerec_worker2.png" +WORKER3_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/facerec_worker3.png" + if project_config.DEBUG_MODE: WORKER1_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/facerec_worker1.png" WORKER2_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/facerec_worker2.png" @@ -191,7 +195,7 @@ WORKER22_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/kepco2_13.jpg" # WORKER16_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/yunikim.jpg" -# WORKER17_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/facerec_worker3.png" +WORKER100_IMG_PATH = PROJECT_PATH + "/AI_ENGINE/DATA/facerec_worker3.png" SIGNAL_INFERENCE = 'inference' SIGNAL_STOP = 'stop' diff --git a/ai_engine_main.py b/ai_engine_main.py index 9aa99dc..ff82136 100644 --- a/ai_engine_main.py +++ b/ai_engine_main.py @@ -23,5 +23,4 @@ from REST_AI_ENGINE_CONTROL.app.common.config import conf if __name__ == '__main__': print('main.py run') - # os.system(". ./rtsp_start.sh") uvicorn.run('REST_AI_ENGINE_CONTROL.app.main:app', host='0.0.0.0', port=conf().REST_SERVER_PORT, reload=True) \ No newline at end of file diff --git a/demo_bi.py b/demo_bi.py index fb7293f..3c3b161 100644 --- a/demo_bi.py +++ b/demo_bi.py @@ -34,7 +34,7 @@ def _bi_sftp_upload(): remotepath = "/home/agics-dev/kepri_storage/rndpartners/" + os.sep + "remote" + '.jpg' - #sftp.put(LOCAL_PATH, remotepath) + sftp.put(LOCAL_PATH, remotepath) sftp.close() transprot.close() diff --git a/main.py b/main.py deleted file mode 100644 index d76e0f9..0000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# 샘플 Python 스크립트입니다. - -# Ctrl+F5을(를) 눌러 실행하거나 내 코드로 바꿉니다. -# 클래스, 파일, 도구 창, 액션 및 설정을 어디서나 검색하려면 Shift 두 번을(를) 누릅니다. - - -def print_hi(name): - # 스크립트를 디버그하려면 하단 코드 줄의 중단점을 사용합니다. - print(f'Hi, {name}') # 중단점을 전환하려면 F9을(를) 누릅니다. - - -# 스크립트를 실행하려면 여백의 녹색 버튼을 누릅니다. -if __name__ == '__main__': - print_hi('PyCharm') - -# https://www.jetbrains.com/help/pycharm/에서 PyCharm 도움말 참조 diff --git a/project_config.py b/project_config.py index 8ff43ee..14d4414 100644 --- a/project_config.py +++ b/project_config.py @@ -10,6 +10,6 @@ CONFIG = CONFIG_MG DEBUG_MODE = False -SFTP_UPLOAD = False +SFTP_UPLOAD = True -FR_UPLOAD = True +FR_UPLOAD = True \ No newline at end of file diff --git a/rtsp_start.sh b/rtsp_start.sh deleted file mode 100644 index dce5783..0000000 --- a/rtsp_start.sh +++ /dev/null @@ -1 +0,0 @@ -mosquitto_pub -p 11883 -h localhost -t ptz -m 1,0.95,0 -u admin -P admin \ No newline at end of file