def on_run_button(self): self.StomataNumber = self.comboBoxStomataNumber.currentText() self.INTERVAL = self.intervalLineEdit.text() self.LOG_LIST.append("StomataNumber:" + self.StomataNumber + " Interval:" + self.INTERVAL + "Model Path:" + self.MODELPATH + "Result video:" + self.OUTPUT_VIDEO_PATH + "CVS Path:" + self.CVS_PATH) self.LOG_StringList.setStringList(self.LOG_LIST) detect(self.FIEPATH, self.OUTPUT_VIDEO_PATH, int(self.INTERVAL), int(self.StomataNumber), self.CVS_PATH, self.MODELPATH, self.output_to_ui)
def kafka_consumer(): #Path of the classes file path_names = 'data/YOLO/rehoboam.names' #Path of the Avro scheme path_avro = "data/scheme.avsc" #Read the class file and transform in dictionary with open(path_names) as f: names_dict = {i: line.split('\n')[0] for i,line in enumerate(f)} #Connection to the Mongo collection to write in mongo_collection = mongoConnect() #Load the tf model saved_model_loaded = tf.saved_model.load( config['weights_tf'], tags=[tag_constants.SERVING]) infer = saved_model_loaded.signatures['serving_default'] #logging.basicConfig(level=logging.INFO) #Read the Avro schema and create the Kafka consumer schema = avro.schema.parse(open(path_avro, "r").read()) reader = DatumReader(schema) consumer = KafkaConsumer('input_image', bootstrap_servers=['localhost:9094', 'localhost:9095'], group_id="rehoboam", value_deserializer=lambda m: decode_avro(m, reader)) for msg in consumer: #Inicialize a dictionary with the class and the number of apperence to 0 default_document = {i: 0 for i in sorted(names_dict.values(), key=lambda x: x)} #Transform the bytes to image img = io.BytesIO(msg.value['image']) img_pil = Image.open(img) img_cv2 = np.array(img_pil) #Call the model to detect the classes scores, classes = detect( parameters=config, infer_model=infer, image=img_cv2) real_classes = [i for i,j in zip(classes[0], scores[0]) if j > 0.0] unique, count = np.unique(real_classes, return_counts=True) result_dict = {names_dict[i]: int(j) for i,j in zip(unique, count)} #Store the number of incidences in the default dictionary for k,v in result_dict.items(): default_document[k] = v result = { 'district_id': int(msg.value['district']), 'camera_id': int(msg.value['camera']), 'timestamp': datetime.now(), 'results': default_document } #Save it in Mongo mongo_collection.insert_one(result).inserted_id consumer.commit()
def test_dev(epoch, test_loader, model, coder, opts): # testing model.eval() check_point = torch.load(os.path.join(opts.save_path, opts.save_file_name) + '.{}.pth.tar'.format(epoch), map_location=device) state_dict = check_point['model_state_dict'] model.load_state_dict(state_dict, strict=True) tic = time.time() is_coco = hasattr(test_loader.dataset, 'coco') # if True the set is COCO else VOC if is_coco: print('COCO dataset evaluation...') evaluator = Evaluator(data_type=opts.data_type) with torch.no_grad(): for idx, datas in enumerate(test_loader): images = datas[0] images = images.to(device) pred = model(images) # eval pred_boxes, pred_labels, pred_scores = detect(pred=pred, coder=coder, opts=opts) img_id = test_loader.dataset.img_id[idx] img_info = test_loader.dataset.coco.loadImgs(ids=img_id)[0] coco_ids = test_loader.dataset.coco_ids info = (pred_boxes, pred_labels, pred_scores, img_id, img_info, coco_ids) evaluator.get_info(info) toc = time.time() if idx % 1000 == 0 or idx == len(test_loader) - 1: print('Step: [{0}/{1}]\t' 'Test Time : {time:.4f}\t' .format(idx, len(test_loader), time=toc - tic)) json.dump(evaluator.results, open('detections_{}_{}_results.json'.format('test-dev2019', opts.submit_name), "w"))
def process_images(execute, show=False, save=True): """Searches for cars in images while varying features""" channels, ssahbs, colors = config.get_channel_ssahb_color(15) imgfns = config.get_images(8) heat_only = False train_big = True xy_windows = config.get_xy_windows(1) t0 = time.time() for ssahb in ssahbs: for channel in channels: for color in colors: tfn, dfn = utils.trained_fn(train_big, ssahb, channel, color) svc, X_scaler = utils.load_trained_svm(tfn) dc = utils.detect(channel, ssahb, color, train_big, heat_only, xy_windows, True, 2, 1) t1 = time.time() for imgfn in imgfns: print(imgfn) for ss in [True]: t2 = time.time() cars = process_image(imgfn, svc, X_scaler, ssahb, channel, color, train_big, dc, heat_only, ss) if show: plt.imshow(cars) plt.show() if save: fn = utils.save_fn(train_big, ssahb, channel, color, ss, imgfn) cv2.imwrite(fn, cv2.cvtColor(cars, cv2.COLOR_BGR2RGB)) t3 = time.time() fmtstr = '{} Seconds to process {} with {} and ss ({})' print(fmtstr.format(round(t3-t2, 1), imgfn, dfn, ss)) if ss: print(lf.log.ss.windows[0:5]) print(lf.log.ss.hot_windows[0:5]) else: print(lf.log.nss.windows[0:5]) print(lf.log.nss.hot_windows[0:5]) print('{} Seconds to process {} images'.format(round(t3-t1, 1), len(imgfns))) fmt_str = '{} Seconds for {} image in {}, {}, {}.' print(fmt_str.format(round(t3-t0, 1), len(imgfns), channels, ssahbs, colors))
def detect_image(): img = request.values['img'] img,imgType = img_base64_decode(img) ''' print(imgType) with open("img."+imgType, "wb") as fh: fh.write(img) ''' image = np.asarray(bytearray(img)) image = cv2.imdecode(image, cv2.IMREAD_COLOR) #print(image.shape) result = utils.detect(image) #print(result['class_names']) #print(result['scores']) rois = result['rois'] dic = {} dic['index']=rois.tolist() r = {'rois': dic['index'],'class_names':result['class_names']} return jsonify(r)
def detect_image(self): # Use Mask-RCNN detect image in lblImg # show result in listResult try: result = utils.detect(self.modify_img) print(result['class_names']) print(result['scores']) print(result['rois']) rois = [[item[0], item[1], item[2], item[3]] for item in result['rois']] self.detect_result = { 'object_names': self.detect_result['object_names'] + result['class_names'], 'rois': self.detect_result['rois'] + rois } self.listResult.clear() self.listResult.addItems(self.detect_result['object_names']) except Exception as e: print("Get Error when detect image.") print("Error message:", e)
def demo(opts, coder, model, img_type='jpg'): img_path_list = glob.glob(os.path.join(opts.img_path, '*' + '.' + img_type)) total_time = 0 with torch.no_grad(): for idx, img_path in enumerate(img_path_list): # --------------------- img load --------------------- img = Image.open(img_path).convert('RGB') img = image_transforms(img, opts.resize).to(device) # --------------------- inference time --------------------- tic = time.time() pred = model(img) pred_boxes, pred_labels, pred_scores = detect(pred, coder, opts, is_demo=True) toc = time.time() inference_time = toc - tic total_time += inference_time if idx % 100 == 0 or idx == len(img_path_list) - 1: # ------------------- check fps ------------------- print('Step: [{}/{}]'.format(idx, len(img_path_list))) print("fps : {:.4f}".format((idx + 1) / total_time)) if opts.data_type == 'voc': label_map = voc_label_map color_array = voc_color_array elif opts.data_type == 'coco': label_map = coco_label_map color_array = coco_color_array if opts.visualization: visualize_results(img, pred_boxes, pred_labels, pred_scores, label_map, color_array, opts.resize)
def video(args): """video detection function""" videos_in = config.get_videos(args.id) channels, ssahbs, colors = config.get_channel_ssahb_color(args.cfg) channel, ssahb, color = utils.check_singles(channels, ssahbs, colors) heat_only = True bs = args.ds == "big" subsample = True heat_threshold = args.thresh history = 20 detect = utils.detect(channel, ssahb, color, bs, heat_only, [], subsample, heat_threshold, history) for video_in in videos_in: video_out = utils.get_video_out(video_in, channel, ssahb, color, bs, heat_threshold, history) print("From =>", video_in, "To =>", video_out) clip2 = VideoFileClip(video_in) clip = clip2.fl_image(detect.get_cars) clip.write_videofile(video_out, audio=False)
def test(epoch, vis, test_loader, model, criterion, coder, opts): # ---------- load ---------- print('Validation of epoch [{}]'.format(epoch)) model.eval() check_point = torch.load( os.path.join(opts.save_path, opts.save_file_name) + '.{}.pth.tar'.format(epoch), map_location=device) state_dict = check_point['model_state_dict'] model.load_state_dict(state_dict) tic = time.time() sum_loss = 0 is_coco = hasattr(test_loader.dataset, 'coco') # if True the set is COCO else VOC if is_coco: print('COCO dataset evaluation...') else: print('VOC dataset evaluation...') evaluator = Evaluator(data_type=opts.data_type) with torch.no_grad(): for idx, datas in enumerate(test_loader): images = datas[0] boxes = datas[1] labels = datas[2] # ---------- cuda ---------- images = images.to(device) boxes = [b.to(device) for b in boxes] labels = [l.to(device) for l in labels] # ---------- loss ---------- pred = model(images) loss, (loc, cls) = criterion(pred, boxes, labels) sum_loss += loss.item() # ---------- eval ---------- pred_boxes, pred_labels, pred_scores = detect(pred=pred, coder=coder, opts=opts) if opts.data_type == 'voc': img_name = datas[3][0] img_info = datas[4][0] info = (pred_boxes, pred_labels, pred_scores, img_name, img_info) elif opts.data_type == 'coco': img_id = test_loader.dataset.img_id[idx] img_info = test_loader.dataset.coco.loadImgs(ids=img_id)[0] coco_ids = test_loader.dataset.coco_ids info = (pred_boxes, pred_labels, pred_scores, img_id, img_info, coco_ids) evaluator.get_info(info) toc = time.time() # ---------- print ---------- if idx % 1000 == 0 or idx == len(test_loader) - 1: print('Epoch: [{0}]\t' 'Step: [{1}/{2}]\t' 'Loss: {loss:.4f}\t' 'Time : {time:.4f}\t'.format(epoch, idx, len(test_loader), loss=loss, time=toc - tic)) mAP = evaluator.evaluate(test_loader.dataset) mean_loss = sum_loss / len(test_loader) print(mAP) print("Eval Time : {:.4f}".format(time.time() - tic)) if vis is not None: # loss plot vis.line( X=torch.ones((1, 2)).cpu() * epoch, # step Y=torch.Tensor([mean_loss, mAP]).unsqueeze(0).cpu(), win='test_loss', update='append', opts=dict(xlabel='step', ylabel='test', title='test loss', legend=['test Loss', 'mAP']))
def __init__(self): self.ctx = zmq.Context() self.createSocket() liveness = CONFIG['HEART_LIVE'] interval = CONFIG['INTERVAL_INIT'] heartbeat = CONFIG['HEART_INTER'] + time.time() storage = os.path.join(CONFIG['STORAGE'], '%s.jpg' % self.identity) sendphoto = CONFIG['SENDPHOTO'] poller = zmq.Poller() poller.register(self.worker, zmq.POLLIN) while True: socks = dict(poller.poll(timeout = CONFIG['HEART_INTER']*1000)) if self.worker in socks: message = self.worker.recv_multipart() if len(message) >= 2: start = time.time() with open(storage, 'wb') as f: f.write(message[4]) cascade = message[3] faces = utils.detect(storage, cascade, sendphoto) logger.debug('NUMBER OF FACES FOUND: %s CASCADE: %s' % (faces, cascade)) if sendphoto and len(faces) != 0: with open(storage, 'rb') as f: message[4] = f.read() else: message[4] = '' message[5] = json.dumps(faces) self.worker.send_multipart(message) stop = time.time() logger.debug('IMAGE PROCESSED IN %s SECONDS' % (stop-start)) elif len(message) == 1 and message[0] == 'HEARTBEAT': logger.debug('HEARTBEAT RECEIVED') liveness = CONFIG['HEART_LIVE'] else: logger.debug('INVALID MESSAGE: %s FROM %s' % (str(message), self.identity)) interval = CONFIG['INTERVAL_INIT'] elif socks == {}: liveness -= 1 if liveness == 0: logger.debug('HEARTBEAT FAILURE: %s' % self.identity) logger.debug('RECONNECTING %s IN %s' % (self.identity, interval)) time.sleep(interval) if interval < CONFIG['INTERVAL_MAX']: interval *= 2 poller.unregister(self.worker) self.worker.close() self.createSocket() poller.register(self.worker, zmq.POLLIN) liveness = CONFIG['HEART_LIVE'] if time.time() > heartbeat: heartbeat = time.time() + CONFIG['HEART_INTER'] logger.debug('HEARTBEAT TO: %s' % self.identity) self.worker.send('HEARTBEAT') self.worker.close() self.ctx.term()
return net if __name__ == '__main__': parser = argparse.ArgumentParser(description='Train network') parser.add_argument("--image-path", type=str, help='Path to validation image folder', default='via-trafficsign/images/val') parser.add_argument( "--device", type=str, help="Choose what device to train, one of the: ['cpu', 'gpu', 'tpu'], tpu is unavailable now", default='cpu') parser.add_argument( '--out-path', type=str, help='Choose logdir for tensorboard logger', default='traffic_logs/training') parser.add_argument('--checkpoint', type=str, help='Path to checkpoint to load from', required=True) parser.add_argument( '--backbone', type=str, help="Choose backbone for centernet, one of the ['mobilenet', 'vgg']", default='vgg') args = parser.parse_args() net = load_model(args) im_path = args.image_path im = cv2.imread(im_path) im = transformer(im) try: pred = detect(net, im) bboxes = decode(pred) im = visualize(im_path, bboxes) plt.imshow(im[:,:,::-1]) plt.show() except: pass
def predict(model, epoch): model.eval() img_path_list = preprossing.get_images(cfg.test_img_path) for index in range(len(img_path_list)): im_fn = img_path_list[index] im = cv2.imread(im_fn)[:, :, ::-1] if im is None: print("can not find image of %s" % (im_fn)) continue print('EAST <==> TEST <==> epoch:{}, idx:{} <==> Begin'.format( epoch, index)) # 图像进行放缩 im_resized, (ratio_h, ratio_w) = utils.resize_image(im) im_resized = im_resized.astype(np.float32) # 图像转换成tensor格式 im_resized = im_resized.transpose(2, 0, 1) im_tensor = torch.from_numpy(im_resized) im_tensor = im_tensor.cuda() # 图像数据增加一维 im_tensor = im_tensor.unsqueeze(0) timer = {'net': 0, 'restore': 0, 'nms': 0} start = time.time() # 输入网络进行推断 score, geometry = model(im_tensor) timer['net'] = time.time() - start # score与geometry转换成numpy格式 score = score.permute(0, 2, 3, 1) geometry = geometry.permute(0, 2, 3, 1) score = score.data.cpu().numpy() geometry = geometry.data.cpu().numpy() # 文本框检测 boxes, timer = utils.detect(score_map=score, geo_map=geometry, timer=timer, score_map_thresh=cfg.score_map_thresh, box_thresh=cfg.box_thresh, nms_thres=cfg.box_thresh) print( 'EAST <==> TEST <==> idx:{} <==> model:{:.2f}ms, restore:{:.2f}ms, nms:{:.2f}ms' .format(index, timer['net'] * 1000, timer['restore'] * 1000, timer['nms'] * 1000)) if boxes is not None: boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h # save to txt file if boxes is not None: res_file = os.path.join( cfg.res_img_path, 'res_{}.txt'.format(os.path.basename(im_fn).split('.')[0])) with open(res_file, 'w') as f: for box in boxes: # to avoid submitting errors box = utils.sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm( box[3] - box[0]) < 5: continue f.write('{},{},{},{},{},{},{},{}\r\n'.format( box[0, 0], box[0, 1], box[1, 0], box[1, 1], box[2, 0], box[2, 1], box[3, 0], box[3, 1], )) cv2.polylines(im[:, :, ::-1], [box.astype(np.int32).reshape((-1, 1, 2))], True, color=(255, 255, 0), thickness=1) print('EAST <==> TEST <==> Save txt at:{} <==> Done'.format( res_file)) # 图片输出 if cfg.write_images: img_path = os.path.join(cfg.res_img_path, os.path.basename(im_fn)) cv2.imwrite(img_path, im[:, :, ::-1]) print('EAST <==> TEST <==> Save image at:{} <==> Done'.format( img_path)) print( 'EAST <==> TEST <==> Record and Save <==> epoch:{}, ids:{} <==> Done' .format(epoch, index))
cifar_classifier.__setstate__(state) # Test the classifier predictions = cifar_classifier.predict(x_test) accuracy = np.sum( np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test) print('Accuracy before attack: {}%'.format(accuracy * 100)) #x_test_adv = load_data('../../data/cifar/fgsm_adversarial.npy') fp = gzip.open('../../data/cifar/fgsm_adversarial.npy', 'rb') x_test_adv = pickle.load(fp) # Test the classifier on adversarial exmaples predictions = cifar_classifier.predict(x_test_adv) accuracy = np.sum( np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test) print('Accuracy after attack: {}%'.format(accuracy * 100)) features_a, features_b, features_c, features_d = get_features( cifar_classifier, x_test_adv) new_accuracy = detect(features_a, features_b, features_c, features_d, d1, d2, d3, d4, x_test_adv) if (new_accuracy != -1): print( 'Accuracy of detection of adversarial samples on CIFAR-10 using FGSM is: {}%' .format(new_accuracy * 100)) #cifar_classifier.save('cifar_fgsm_state_dict', 'models') print('exiting cifar-fast gradient sign method...')
cline.add_argument('-nms_thold', type=float, default=0.4, help='threshold for non max supression') cline.add_argument('-model_res', type=int, default=416, help='resolution of the model\'s input') if __name__ == '__main__': args = cline.parse_args() with torch.no_grad(): bbone = Darknet() bbone = bbone.extractor model = Yolo3(bbone) print(f'Loading weights from {args.weights}') model.load_state_dict(torch.load(args.weights)) model.to(device) cap = cv2.VideoCapture(0) while(True): _, image = cap.read() image = cv2.resize(image, (416, 416)) res = detect(model, image, device, args.obj_thold, args.nms_thold, args.model_res) cv2.imshow('webcam', res) k = cv2.waitKey(100) if k == 27: # Press Esc to quit break cap.release() cv2.destroyAllWindows()
def main(argv=None): os.environ['CUDA_VISIBLE_DEVICES'] = gpu_list #whether we have detcted the same number as the input number_correct = 0 time1 = time.time() with tf.get_default_graph().as_default(): input_images = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_images') global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False) f_score, f_geometry = model.model(input_images, is_training=False) #use exonential moving average variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step) saver = tf.train.Saver(variable_averages.variables_to_restore()) with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: #load weights,biases,gradients and other variables ckpt_state = tf.train.get_checkpoint_state(checkpoint_path) #load the sece text detection model model_path = os.path.join(checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path)) saver.restore(sess, model_path) print("processing starts") duration_start = time.time() #read the frame of the input video vs = cv2.VideoCapture(FLAGS.test_data_path) fps = FPS().start() frame_count = 0 large = 0 while True: #number is not verified not_verified = 1 processing_time = time.time()-duration_start print(processing_time) frame_count = frame_count + 1 frame = vs.read() frame = frame[1] if frame is None: break pill_ready = 0 # We start the number recognition when the pill is put in front of the mouth; Before that, we downsize the frame to improve the efficiency if large == 1: frame = imutils.resize(frame, height = 1500) #if there's hand holding the pill in fornt of the mouth ,we enlarge the frame and do scene text detection and reocgnition else: frame = imutils.resize(frame, height=200) im = frame[:, :, ::-1] orig = frame.copy() image_np = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #detect hands and get the scores of the detected hands boxes1, scores1 = detector_utils.detect_objects(image_np, detection_graph, sess1) h, w = im.shape[:2] #draw a box around the hands whose score is greater than the score_thresh hands_detected = detector_utils.draw_box_on_image(num_hands_detect, score_thresh, scores1, boxes1, w, h, im[:, :, ::-1]) # if there's no hand holding the pill, we do not process the fraem if hands_detected==0: pass if len(face_recognition.face_locations(orig)) == 0: pass elif len(face_recognition.face_locations(orig)) > 1: pass else: #face detection face_location = face_recognition.face_locations(orig) top, right, bottom, left = face_location[0] cv2.rectangle(im[:, :, ::-1], (left, top), (right, bottom), (0, 0, 255)) #we do number and letter detection inside the face area roi_text = orig[top:bottom, left:right] try: #mouth detection (x, y, w, h) = mouth_detection.mouth_detection_video(orig, detector, predictor) cv2.rectangle(im[:, :, ::-1], (x, y), (x + w, y + h), (0, 0, 255)) d = int(0.35 * h) roi = orig[y + d:y + h, x:x + w] global px, py, pw, ph #pill detection inside the mouth (px, py, pw, ph) = utils.color_detection_white(roi) #pill detected if (pw != 0): cv2.rectangle(im[:, :, ::-1], (x + px, y + py + d), (x + px + pw, y + py + ph + d), (0, 255, 0), font_thickness) large = 1 except: pass if (number_correct == 0)&(large == 1): try: start_time = time.time() #resize the area of number and letter detection im_resized, (ratio_h, ratio_w) = utils.resize_image(roi_text) timer = {'net': 0, 'restore': 0, 'nms': 0} start = time.time() score, geometry = sess.run([f_score, f_geometry], feed_dict={input_images: [im_resized]}) timer['net'] = time.time() - start #detect the number and letter, and get the boxes which contains the location of the number or letter boxes, timer = utils.detect(score_map=score, geo_map=geometry, timer=timer) #if the box is not none, resize the box for further recognition if boxes is not None: boxes = boxes[:, :8].reshape((-1, 4, 2)) boxes[:, :, 0] /= ratio_w boxes[:, :, 1] /= ratio_h if boxes is not None: for indBoxes, box in enumerate(boxes): #number recognition by morphology processing and tesseract(the function was written in utils.py) text = utils.recognize_to_text(roi_text[:, :, ::-1], box) #if any number or letter has been detected, we set the not_berified to 0 if text is not None: not_verified = 0 #print("[recognize box({})] text: {}".format(indBoxes, text)) box = utils.sort_poly(box.astype(np.int32)) if np.linalg.norm(box[0] - box[1]) < 5 or np.linalg.norm( box[3] - box[0]) < 5: # strip small box continue cv2.putText(im[:, :, ::-1], text, (50, 250), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) if text == FLAGS.expected: number_correct = 1 except: pass time1 = time.time() - time1 time2 = time.time() - duration_start #if the time of number or letter recognition is greater than certain amount of time, stop processing if time2>180: break #if we detect the same number as the input, stop processing if (number_correct == 1): break #cv2.imshow("im", im[:, :, ::-1]) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() fps.stop() vs.release() #if we detect the same number or letter as the input if number_correct==1: print("Number read is", text, ", which corresponds correctly to the pill that was dispensed.") sys.stdout.flush() #if we didn't detect any number or letter elif (not_verified == 1): print("The number is not verified") sys.stdout.flush() #if we didn't detect the right number or letter else: print("Number read is",text,", which is different from the number on the pill. We will check this manually") sys.stdout.flush() print("video processing time",time2)