def train(): # Config params image_shape = (224, 224) batch_size = 64 epochs = 30 # Dataset & model detector = Detector(image_shape) ds = Dataset(image_shape, batch_size) training_pipeline, validation_pipeline = ds.pipeline() steps_per_epoch = ds.num_training//batch_size # Start training model_history = detector.train( training_pipeline, epochs, steps_per_epoch, validation_pipeline, ) # Visualize loss loss = model_history.history['loss'] val_loss = model_history.history['val_loss'] range_of_epochs = range(epochs) plt.figure() plt.plot(range_of_epochs, loss, 'r', label='Training loss') plt.plot(range_of_epochs, val_loss, 'bo', label='Validation loss') plt.title('Training Loss and Validation Loss') plt.xlabel('Epoch') plt.ylabel('Loss Value') plt.ylim([0, 1]) plt.legend() plt.show()
def show_predictions(): image_shape = (224, 224) detector = Detector(image_shape) ds = Dataset(image_shape) pipeline, _ = ds.pipeline() for image, mask in pipeline.take(1): pred_mask = detector.predict(image) __display([image[0], mask[0], __create_mask(pred_mask)])
def demo(): detector = Detector(opt) image_path = opt.image print("input image path:", image_path) image = cv2.imread(image_path) ret = detector.run(image) save_results(opt, image, ret['results'], 'demo_result.png') time_str = '' for stat in ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge']: time_str += f'{stat} {ret[stat]:.3f}s |' print(time_str)
def summary(): detector = Detector(image_shape=(224, 224)) tf.keras.utils.plot_model(detector.model, show_shapes=True) img = cv.imread('model.png') img = cv.resize(img, (512, 768)) cv.imshow('Model summary', img) cv.waitKey()
def convert(): # Load model image_shape = (224, 224) detector = Detector(image_shape, 'models') model = detector.model # Data pipeline batch_size = 64 ds = Dataset(image_shape, batch_size) pipeline, _ = ds.pipeline() def representative_dataset_gen(): for tensor in pipeline.take(1): raw_imgs, mask_imgs = tensor img = np.array([raw_imgs[0]]) yield [img] # Shape (1, height, width, channel) converter = tf.lite.TFLiteConverter.from_keras_model(model) converter.optimizations = [tf.lite.Optimize.DEFAULT] converter.representative_dataset = representative_dataset_gen converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] converter.inference_input_type = tf.uint8 converter.inference_output_type = tf.uint8 tflite_quant_model = converter.convert() MODEL = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../models/tpu/ohmnilabs_floornet_224_quant_postprocess.tflite') open(MODEL, 'wb').write(tflite_quant_model)
def predict(): # Config image_shape = (224, 224) output_shape = (640, 480) alpha = 0.5 current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") out = cv.VideoWriter( 'dist/floorNet-%s.avi' % current_time, cv.VideoWriter_fourcc(*'DIVX'), 10, output_shape) # Model detector = Detector(image_shape, 'models') # Image source cam = Camera() stream = cam.get_stream() # Prediction while True: start = time.time() print("======================================") img = stream.get() img = detector.normalize(img) mask = detector.predict(img) mask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR) cv.addWeighted(mask, alpha, img, 1-alpha, 0, img) img = cv.resize(img, output_shape) cv.imshow('Camera', img) # Save video frame = (img*255).astype(np.uint8) out.write(frame) # Calculate frames per second (FPS) end = time.time() print('Total estimated time: {:.4f}'.format(end-start)) fps = 1/(end-start) print("FPS: {:.1f}".format(fps)) if cv.waitKey(10) & 0xFF == ord('q'): break # Clear resources out.release() cv.destroyAllWindows() cam.terminate()
def __init__(self): # Capture print('Loading stream') self.stream_capture = AsyncCapture(AppConfig.stream_url, 1 / 12, 2) # Detector print('Compiling and loading model') self.detector = Detector( 1280, 720, 16, os.path.join(os.path.dirname(__file__), "data", "checkpoints"), (NNConfig.human_threshold, NNConfig.car_threshold)) self.detector.compile_model() self.detector.restore_model() print(self.detector.model.summary()) # MQTT print('Loading MQTT client') self.mqtt = MQTTClient(AppConfig.mqtt_username) self.mqtt.connect() print('Ready')
def test(image_file): image = cv2.imread(image_file) detector = Detector() detector.load() recognizer = Recognizer() recognizer.load() roi, _, _, _ = detector.process(image) for i, img in enumerate(roi): gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # blur = cv2.medianBlur(gray, 5) # thresh = cv2.adaptiveThreshold( # blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) text, _, _ = recognizer.process(gray) out_file = '%s/%.2d_%s.jpg' % (OUTPUT_DIR, i, text) cv2.imwrite(out_file, gray) print('Saved OCR result to "%s" folder' % OUTPUT_DIR)
def behaviourDetect(): args = parse_args() if args.cpu: ctx = mx.cpu() else: ctx = mx.gpu(args.gpu_id) # parse image list image_list = ['messigray.png'] assert len(image_list) > 0, "No valid image specified to detect" prefix = args.prefix + args.network network = None detector = Detector(network, prefix, args.epoch, args.data_shape, (args.mean_r, args.mean_g, args.mean_b), ctx=ctx) # run detection global isNotQuit global numOfStanding global numOfSitting global numOfLying a = True kcf = False cap = cv2.VideoCapture(args.video) #cap = cv2.VideoCapture(0) objects = [] pre_objects = [] fobjects = [] ret, frame = cap.read() height, width = frame.shape[:2] frame = cv2.resize(frame, (width / 2, height / 2)) pre_Frame = frame cv2.imwrite('messigray.png', frame) test_iter = detector.im_detect(image_list, args.dir, args.extension, show_timer=args.show_timer) font = cv2.FONT_HERSHEY_SIMPLEX while (isNotQuit): start = timer() #KCF track if (kcf): ret1, framekcf = cap.read() if not ret1: break height, width = framekcf.shape[:2] framekcf = cv2.resize(framekcf, (width / 2, height / 2)) st = 0 sit = 0 ly = 0 for objecta in (objects): tracker = KCF.kcftracker( False, True, False, False) #hog, fixed_window, multiscale, lab tracker.init([ objecta.xmin, objecta.ymin, objecta.xmax - objecta.xmin, objecta.ymax - objecta.ymin ], pre_Frame) pre_Frame = framekcf boundingbox = tracker.update(framekcf) boundingbox = map(int, boundingbox) cv2.rectangle(framekcf, (boundingbox[0], boundingbox[1]), (boundingbox[0] + boundingbox[2], boundingbox[1] + boundingbox[3]), (255, 255, 0), 1) if objecta.label == 0: st += 1 if objecta.label == 1: sit += 1 if objecta.label == 2: ly += 1 cv2.putText(framekcf, CLASSES[objecta.label] + str(objecta.score), (boundingbox[0], boundingbox[1]), font, 0.3, (255, 255, 255), 1, cv2.LINE_AA) framekcf = cv2.resize(framekcf, (width, height)) numOfStanding = st numOfSitting = sit numOfLying = ly #~KCF track #pre process next frame ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, (width / 2, height / 2)) pre_Frame = frame cv2.imwrite('messigray.png', frame) test_iter = detector.im_detect(image_list, args.dir, args.extension, show_timer=args.show_timer) cv2.imshow("img", framekcf) kcf = False pre_objects = objects objects = [] #~pre process next frame else: #detection every 2 frame dets = detector.detect(test_iter, args.show_timer) #visualize detection for k, det in enumerate(dets): height = frame.shape[0] width = frame.shape[1] for i in range(det.shape[0]): cls_id = int(det[i, 0]) if cls_id >= 0: score = det[i, 1] if score > args.thresh: xmin = int(det[i, 2] * width) ymin = int(det[i, 3] * height) xmax = int(det[i, 4] * width) ymax = int(det[i, 5] * height) cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 255, 255), 1) class_name = str(cls_id) if CLASSES and len(CLASSES) > cls_id: class_name = CLASSES[cls_id] objecta = Object(xmin, ymin, xmax, ymax, cls_id, score, 1) objects.append(objecta) fobjects = [] #filter object overlap for aa in range(len(objects)): for bb in range(aa + 1, len(objects)): iou1 = iou_fiter([ objects[aa].xmin, objects[aa].ymin, objects[aa].xmax, objects[aa].ymax ], [ objects[bb].xmin, objects[bb].ymin, objects[bb].xmax, objects[bb].ymax ]) if iou1 > 0.6 and iou1 <= 1: if objects[aa].score > objects[bb].score: fobjects.append(objects[bb]) else: fobjects.append(objects[aa]) for objecta in (fobjects): try: objects.remove(objecta) except: print ' ' #~filter object overlap #correct object label for aa in range(len(objects)): for bb in range(len(pre_objects)): iou1 = iou([ objects[aa].xmin, objects[aa].ymin, objects[aa].xmax, objects[aa].ymax ], [ pre_objects[bb].xmin, pre_objects[bb].ymin, pre_objects[bb].xmax, pre_objects[bb].ymax ]) if iou1 > 0.6 and iou1 <= 1 and objects[ aa].label != pre_objects[bb].label: objects[aa].newlabel = pre_objects[bb].newlabel + 1 if objects[aa].newlabel <= 14: objects[aa].label = pre_objects[bb].label else: objects[aa].newlabel = 1 #~correct object label st = 0 sit = 0 ly = 0 for objecta in (objects): cv2.rectangle(frame, (objecta.xmin, objecta.ymin), (objecta.xmax, objecta.ymax), (0, 255, 0), 1) if objecta.label == 0: st += 1 if objecta.label == 1: sit += 1 if objecta.label == 2: ly += 1 cv2.putText(frame, CLASSES[objecta.label] + str(objecta.score), (objecta.xmin, objecta.ymin), font, 0.3, (255, 255, 255), 1, cv2.LINE_AA) #~visualize detection frame = cv2.resize(frame, (width * 2, height * 2)) #resize frame cv2.imshow("img", frame) #show video numOfStanding = st numOfSitting = sit numOfLying = ly kcf = True #~detection time_elapsed = timer() - start #print("Detection timessssss for {} images: {:.4f} sec fps {:.4f}".format( # 1, time_elapsed, 1/time_elapsed)) k = cv2.waitKey(1) & 0xFF if k == ord('q'): isNotQuit = False break cap.release() cv2.destroyAllWindows()
def main() -> None: parser = argparse.ArgumentParser( description="detect object from various media") parser.add_argument( '--media', type=str, default=None, help=('filename of image/video' ' (if not set, use streaming video from camera)')) parser.add_argument('--height', type=int, default=720, help='camera image height') parser.add_argument('--width', type=int, default=1280, help='camera image width') parser.add_argument( '--hflip', action='store_false' if DEFAULT_HFLIP else 'store_true', help='flip horizontally') parser.add_argument( '--vflip', action='store_false' if DEFAULT_VFLIP else 'store_true', help='flip vertically') parser.add_argument('--model', type=str, default='ssd', choices=[ 'ssd', 'face', 'yolov3-tiny', 'yolov3', 'yolov3-spp', 'yolov4-tiny', 'yolov4', 'yolov5s', 'yolov5m', 'yolov5l', 'yolov5x' ], help='object detection model') parser.add_argument('--quant', type=str, default='fp32', choices=['fp32', 'fp16', 'int8', 'tpu'], help='quantization mode (or use EdgeTPU)') parser.add_argument( '--target', type=str, default='all', help='the target type of detecting object (default: all)') parser.add_argument('--iou-threshold', type=float, default=0.45, help='the IoU threshold of NMS') parser.add_argument('--conf-threshold', type=float, default=0.5, help='the confidence score threshold of NMS') parser.add_argument('--fontsize', type=int, default=20, help='fontsize to display') parser.add_argument('--fastforward', type=int, default=1, help=('frame interval for object detection' ' (default: 1 = detect every frame)')) args = parser.parse_args() config = Config(**vars(args)) detector = Detector(config=config) detector.run() return
def run_demo(args): skip_frames = args.skip_frames out_fps = args.out_fps sigma_iou = args.sigma_iou log = args.log in_video_path = args.in_video_path device = args.device max_miss_frames = 3 min_frame_th = 3 video_name = in_video_path.split('/')[-1].split('.')[0] # setup experiment directory if not os.path.exists('runs'): os.makedirs('runs') exp_id = len(os.listdir('runs')) exp_dir = os.path.join('runs', 'exp_' + str(exp_id)) os.mkdir(exp_dir) violation_dir = os.path.join(exp_dir, 'violations') os.mkdir(violation_dir) print("Experiment Directory: ", exp_dir) print('==== Configuration ====') print(args) # load models model_od = 'models/mobilenet_ssd/FP16/mobilenet-ssd.xml' mode_pose = 'models/pose_estimation/FP16/single-human-pose-estimation-0001.xml' cls_file = 'models/pose_classifier/classifier.sav' ie = IECore() detector_person = Detector(ie, path_to_model_xml=model_od, device=device, label_class=15) single_human_pose_estimator = HumanPoseEstimator( ie, path_to_model_xml=mode_pose, device=device) classifier = pickle.load(open(cls_file, 'rb')) #read video file cap = cv2.VideoCapture(in_video_path) ret, frame = cap.read() # output video out = cv2.VideoWriter(os.path.join(exp_dir, video_name + '.avi'), cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), out_fps, (frame.shape[1], frame.shape[0])) #time benchmarks total_time = 0 detector_time = 0 pose_time = 0 classification_time = 0 tracking_time = 0 operation_count = 0 tracks_active = [] t_id = 1 frame_i = 0 while (cap.isOpened()): # read a frame from video ret, frame = cap.read() frame_i += 1 # if valid frame read if ret == True: # skip frames if frame_i % skip_frames == 0: operation_count += 1 start_time = time.time() if log: print("====== Frame id : ", str(frame_i)) # detect person s = time.time() boxes = detector_person.detect(frame) detector_time += time.time() - s # extract pose s = time.time() key_points = [ single_human_pose_estimator.estimate(frame, bbox) for bbox in boxes ] pose_time += time.time() - s if log: print("Detections : ", str(len(key_points))) # predict state and get detections s = time.time() detections_frame = [] for box, k_p in zip(boxes, key_points): features = preprocess(k_p) state = classifier.predict(features) det = Detection(box=box, state=state, frame=frame_i) detections_frame.append(det) classification_time += time.time() - s dets = detections_frame # person tracking s = time.time() updated_tracks = [] for track in tracks_active: if len(dets) > 0: best_match = max( dets, key=lambda x: iou(track.position, x.box)) if iou(track.position, best_match.box) >= sigma_iou: track.update(best_match.box, best_match.state, frame_i, frame) updated_tracks.append(track) # remove from best matching detection from detections del dets[dets.index(best_match)] # if track was not updated if len(updated_tracks ) == 0 or track is not updated_tracks[-1]: # finish track when the conditions are met track.miss_track(frame_i) if track.miss_count < max_miss_frames: updated_tracks.append(track) # create new tracks new_tracks = [] for det in dets: new_tracks.append( Track(det.box, det.state, det.frame, frame_i, t_id, violation_dir)) t_id += 1 tracks_active = updated_tracks + new_tracks tracking_time += time.time() - s if log: print("Active Tracks : ", str(len(tracks_active))) valid_tracks = [ t for t in tracks_active if t.frame_count() > min_frame_th ] frame = draw_tracks(valid_tracks, frame) # save results out.write(frame) total_time += time.time() - start_time else: break cap.release() print("======= FPS Report =======") print("Total fps: " + str(float(operation_count) / total_time)) print("Detector fps: " + str(float(operation_count) / detector_time)) print("Pose estimation fps: " + str(float(operation_count) / pose_time)) print("Pose classification fps: " + str(float(operation_count) / classification_time)) print("Person Tracker fps: " + str(float(operation_count) / tracking_time))
def test_visdrone(): torch.manual_seed(317) logger = Logger(opt, "test") dataset = Dataset(opt, "val") detector = Detector(opt) time_stats = ['tot', 'load', 'pre', 'net', 'dec', 'post', 'merge'] avg_time_stats = {t: AverageMeter() for t in time_stats} all_gt = [] all_det = [] allheight = [] allwidth = [] detections = [] for iter_id in range(len(dataset)): img_id = dataset.images[iter_id] img_info = dataset.coco.loadImgs(ids=[img_id])[0] img_path = os.path.join(dataset.img_dir, img_info['file_name']) image = cv2.imread(img_path) ret = detector.run(image) # det # convert_eval_format det = [] for cls_ind, bboxs in ret['results'].items(): category_id = dataset._valid_ids[cls_ind - 1] for bbox in bboxs: bbox[2] -= bbox[0] bbox[3] -= bbox[1] for i in range(4): bbox[i] = round(bbox[i], 2) score = round(bbox[4], 2) # coco detections.append({ "image_id": int(img_id), "category_id": int(category_id), "bbox": bbox[0:4], "score": score, }) det.append([ bbox[0], bbox[1], bbox[2], bbox[3], score, category_id, -1, -1 ]) # f.write(f"{bbox[0]},{bbox[1]},{bbox[2]},{bbox[3]},{score},{category_id},{-1},{-1}\n") det = np.array(det) # gt label = [] ann_ids = dataset.coco.getAnnIds(imgIds=[iter_id]) anns = dataset.coco.loadAnns(ids=ann_ids) for ann in anns: bbox = ann['bbox'] category_id = ann['category_id'] score = 0 if category_id == 0 or category_id == 11 else 1 label.append([ bbox[0], bbox[1], bbox[2], bbox[3], score, category_id, -1, -1 ]) label = np.array(label) height, width = image.shape[:2] allheight.append(height) allwidth.append(width) all_det.append(det) all_gt.append(label) info = f'[{iter_id}/{len(dataset)}]' for t in avg_time_stats: avg_time_stats[t].update(ret[t]) info += '|{} {tm.val:.3f}s ({tm.avg:.3f}s) '.format( t, tm=avg_time_stats[t]) # log if iter_id % 50 == 0: logger.write(info) # visdrone eval ap_all, ap_50, ap_75, ar_1, ar_10, ar_100, ar_500, ap_classwise = eval_det( all_gt, all_det, allheight, allwidth, per_class=True) logger.write(f'AP [IoU=0.50:0.95 | maxDets=500] = {ap_all:3.2f}%.') logger.write(f'AP [IoU=0.50 | maxDets=500] = {ap_50:3.2f}%.') logger.write(f'AP [IoU=0.75 | maxDets=500] = {ap_75:3.2f}%.') logger.write(f'AR [IoU=0.50:0.95 | maxDets= 1] = {ar_1:3.2f}%.') logger.write(f'AR [IoU=0.50:0.95 | maxDets= 10] = {ar_10:3.2f}%.') logger.write(f'AR [IoU=0.50:0.95 | maxDets=100] = {ar_100:3.2f}%.') logger.write(f'AR [IoU=0.50:0.95 | maxDets=500] = {ar_500:3.2f}%.') for i, ap in enumerate(ap_classwise): logger.write( f'Class {opt.dataset_info["class_name"][i]:15} AP = {ap:3.2f}%')
def behaviourDetect(): #zig = Zigbee() #mms_count = 100 detect_lying = False #mms = MMS() args = parse_args() if args.cpu: ctx = mx.cpu() else: ctx = mx.gpu(args.gpu_id) # parse image list image_list = ['messigray.png'] assert len(image_list) > 0, "No valid image specified to detect" prefix = args.prefix + args.network network = None detector = Detector(network, prefix, args.epoch, args.data_shape, (args.mean_r, args.mean_g, args.mean_b), ctx=ctx) # run detection global isNotQuit global numOfStanding global numOfSitting global numOfLying outRects = [[674, 0, 960, 245], [561, 370, 960, 540], [718, 217, 960, 466]] horRects = [[415, 84, 547, 439]] verRects = [[315, 194, 642, 344]] maxarea = 30000 minside = 125 a = True kcf = False cap = cv2.VideoCapture(args.video) #cap = cv2.VideoCapture(0) objects = [] pre_objects = [] fobjects = [] ret, frame = cap.read() cap.set(3, 1920) cap.set(4, 1080) frame = cv2.flip(frame, 1) angle = 30 frame = imutils.rotate(frame, angle) frame = cv2.flip(frame, 0) height, width = frame.shape[:2] frame = cv2.resize(frame, (width / 2, height / 2)) pre_Frame = frame cv2.imwrite('messigray.png', frame) test_iter = detector.im_detect(image_list, args.dir, args.extension, show_timer=args.show_timer) font = cv2.FONT_HERSHEY_SIMPLEX while (isNotQuit): detect_lying = False #if(mms_count < 100): # mms_count += 1 start = timer() #KCF track if (kcf): ret1, framekcf = cap.read() '''framekcf = cv2.flip( framekcf, 1 ) angle = 30 framekcf = imutils.rotate(framekcf, angle) framekcf = cv2.flip( framekcf, 0 )''' #cap.set(3,1920) #cap.set(4,1080) if not ret1: break height, width = framekcf.shape[:2] framekcf = cv2.resize(framekcf, (width / 2, height / 2)) st = 0 sit = 0 ly = 0 for objecta in (objects): tracker = KCF.kcftracker( False, True, False, False) #hog, fixed_window, multiscale, lab tracker.init([ objecta.xmin, objecta.ymin, objecta.xmax - objecta.xmin, objecta.ymax - objecta.ymin ], pre_Frame) pre_Frame = framekcf boundingbox = tracker.update(framekcf) boundingbox = map(int, boundingbox) xmin, ymin, xmax, ymax = boundingbox[0], boundingbox[1], ( boundingbox[0] + boundingbox[2]), (boundingbox[1] + boundingbox[3]) print('fdfdsg', float(ymax - ymin) / (xmax - xmin)) if isInRects([(xmin + xmax) / 2, (ymax + ymin) / 2], outRects) or ((xmax - xmin) * (ymax - ymin) > maxarea): continue #filter the big box or box is in incorrect position if objecta.label == 0: if isInRects([(xmin + xmax) / 2, (ymax + ymin) / 2], verRects) and (ymax - ymin) > 125 and ( ymax - ymin) / (xmax - xmin) > 1.3: objecta.label = 2 elif isInRects([(xmin + xmax) / 2, (ymax + ymin) / 2], horRects) and (xmax - xmin) > 125 and ( xmax - xmin) / (ymax - ymin) > 1.3: objecta.label = 2 else: st += 1 if objecta.label == 1: sit += 1 if objecta.label == 2: ly += 1 cv2.rectangle(framekcf, (xmin, ymin), (xmax, ymax), colors[objecta.label], 1) cv2.putText(framekcf, CLASSES[objecta.label], (xmin, ymin), font, 0.5, colors[objecta.label], 1, cv2.LINE_AA) #framekcf = cv2.resize(framekcf,(width,height)) numOfStanding = st numOfSitting = sit numOfLying = ly #~KCF track #pre process next frame ret, frame = cap.read() '''frame = cv2.flip( frame, 1 ) angle = 30 frame = imutils.rotate(frame, angle) frame = cv2.flip( frame, 0 )''' #cap.set(3,1920) #cap.set(4,1080) if not ret: break frame = cv2.resize(frame, (width / 2, height / 2)) pre_Frame = frame cv2.imwrite('messigray.png', frame) test_iter = detector.im_detect(image_list, args.dir, args.extension, show_timer=args.show_timer) cv2.imshow("img", framekcf) kcf = False pre_objects = objects objects = [] #~pre process next frame else: #detection every 2 frame dets = detector.detect(test_iter, args.show_timer) #visualize detection for k, det in enumerate(dets): height = frame.shape[0] width = frame.shape[1] for i in range(det.shape[0]): cls_id = int(det[i, 0]) if cls_id >= 0: score = det[i, 1] if score > args.thresh: xmin = int(det[i, 2] * width) ymin = int(det[i, 3] * height) xmax = int(det[i, 4] * width) ymax = int(det[i, 5] * height) #cv2.rectangle(frame,(xmin,ymin),(xmax, ymax),(0,255,255),1) class_name = str(cls_id) if CLASSES and len(CLASSES) > cls_id: class_name = CLASSES[cls_id] #zig.count_detec(cls_id) objecta = Object(xmin, ymin, xmax, ymax, cls_id, score, 1) objects.append(objecta) fobjects = [] #t = threading.Thread(target=zig.send_zigbee, args=()) #t.start() #filter object overlap for aa in range(len(objects)): for bb in range(aa + 1, len(objects)): iou1 = iou_fiter([ objects[aa].xmin, objects[aa].ymin, objects[aa].xmax, objects[aa].ymax ], [ objects[bb].xmin, objects[bb].ymin, objects[bb].xmax, objects[bb].ymax ]) if iou1 > 0.6 and iou1 <= 1: if objects[aa].score > objects[bb].score: fobjects.append(objects[bb]) else: fobjects.append(objects[aa]) for objecta in (fobjects): try: objects.remove(objecta) except: print ' ' #~filter object overlap #correct object label for aa in range(len(objects)): for bb in range(len(pre_objects)): iou1 = iou([ objects[aa].xmin, objects[aa].ymin, objects[aa].xmax, objects[aa].ymax ], [ pre_objects[bb].xmin, pre_objects[bb].ymin, pre_objects[bb].xmax, pre_objects[bb].ymax ]) if iou1 > 0.6 and iou1 <= 1 and objects[ aa].label != pre_objects[bb].label: objects[aa].newlabel = pre_objects[bb].newlabel + 1 if objects[aa].newlabel <= 14: objects[aa].label = pre_objects[bb].label else: objects[aa].newlabel = 1 #~correct object label st = 0 sit = 0 ly = 0 for objecta in (objects): xmin, ymin, xmax, ymax = objecta.xmin, objecta.ymin, objecta.xmax, objecta.ymax if isInRects([(xmin + xmax) / 2, (ymax + ymin) / 2], outRects) or ((xmax - xmin) * (ymax - ymin) > maxarea): continue if objecta.label == 0: if isInRects([(xmin + xmax) / 2, (ymax + ymin) / 2], verRects) and (ymax - ymin) > 125 and ( ymax - ymin) / (xmax - xmin) > 1.3: objecta.label = 2 elif isInRects([(xmin + xmax) / 2, (ymax + ymin) / 2], horRects) and (xmax - xmin) > 125 and ( xmax - xmin) / (ymax - ymin) > 1.3: objecta.label = 2 else: st += 1 if objecta.label == 1: sit += 1 if objecta.label == 2: detect_lying = True ly += 1 cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), colors[objecta.label], 1) cv2.putText(frame, CLASSES[objecta.label], (xmin, ymin), font, 0.5, colors[objecta.label], 1, cv2.LINE_AA) #~visualize detection frame = cv2.resize(frame, (width, height)) #resize frame cv2.imshow("img", frame) #show video numOfStanding = st numOfSitting = sit numOfLying = ly '''if(detect_lying and mms_count == 100): mms_count = 0 frame_mms = cv2.resize(frame,(420,320)) cv2.imwrite('mms_save.png',frame_mms) tt = threading.Thread(target=mms.send_mms, args=()) tt.start()''' kcf = True #~detection time_elapsed = timer() - start #print("Detection timessssss for {} images: {:.4f} sec fps {:.4f}".format( # 1, time_elapsed, 1/time_elapsed)) k = cv2.waitKey(1) & 0xFF if k == ord('q'): isNotQuit = False break cap.release() cv2.destroyAllWindows()
## WRITE IMAGE's NAMES TO FILE ## file1 = open(out_dir+'/'+list_images_name,'w') for f in os.listdir(image_dir): image_list.append(os.path.join(image_dir, f)) file1.write(f.split('.')[0]+'\n') file1.close() ##~ WRITE IMAGE's NAMES TO FILE ## filePerson = open(out_dir+'/out_person.txt','w') fileBicycle = open(out_dir+'/out_bicycle.txt','w') fileDog = open(out_dir+'/out_dog.txt','w') fileCat = open(out_dir+'/out_cat.txt','w') fileCar = open(out_dir+'/out_car.txt','w') network = None detector = Detector(network, prefix, epoch, data_shape, (mean_r, mean_g, mean_b), ctx=ctx) dets = detector.im_detect(image_list, show_timer=True) assert len(dets) == len(image_list) if os.path.exists(out_dir+'/'+out_image_folder): shutil.rmtree(out_dir+'/'+out_image_folder) os.makedirs(out_dir+'/'+out_image_folder) for k, det in enumerate(dets): img = cv2.imread(image_list[k]) height = img.shape[0] width = img.shape[1] for i in range(det.shape[0]): cls_id = int(det[i, 0]) if cls_id in [1,6,7,11,14]: score = det[i, 1] if score > thresh: