def main(_argv): class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') dataset = load_tfrecord_dataset(FLAGS.dataset, FLAGS.classes, FLAGS.size) dataset = dataset.shuffle(512) for image, labels in dataset.take(1): boxes = [] scores = [] classes = [] for x1, y1, x2, y2, label in labels: if x1 == 0 and x2 == 0: continue boxes.append((x1, y1, x2, y2)) scores.append(1) classes.append(label) nums = [len(boxes)] boxes = [boxes] scores = [scores] classes = [classes] logging.info('labels:') for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) img = cv2.cvtColor(image.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(FLAGS.output, img) logging.info('output saved to: {}'.format(FLAGS.output))
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) model = tf.saved_model.load(FLAGS.model) infer = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY] logging.info(infer.structured_outputs) class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('Loaded classes') img_raw = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) outputs = infer(img) boxes, scores, classes, nums = outputs["yolo_nms"], outputs[ "yolo_nms_1"], outputs["yolo_nms_2"], outputs["yolo_nms_3"] logging.info('detections:') for i in range(nums[0]): if (scores[0][i] < 0.3): continue logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], scores[0][i].numpy(), boxes[0][i].numpy())) img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(FLAGS.output, img) logging.info('output saved to: {}'.format(FLAGS.output))
def main(_argv): yolo = YoloV3() yolo.load_weights(FLAGS.weights) logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') img = tf.image.decode_jpeg(open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img, 0) img = transform_images(img, size) tf.print(img) t1 = time.time() boxes, scores, classes, nms = yolo(img) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('detections:') tf.print(nms) tf.print(boxes, scores, classes, nms) # range_nms = nms[0].to_int64 for i in range(1): print("Did I enter? ") print("class_name: ", class_names[int(classes[0][i])]) logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) img = cv2.imread(FLAGS.image) img = draw_outputs(img, (boxes, scores, classes, nms), class_names) cv2.imwrite(FLAGS.output, img) logging.info('output saved to: {}'.format(FLAGS.output))
def main(args): # 1、初始化模型并加载权重 if args.tiny: yolo = YoloV3Tiny(classes=args.num_classes) else: yolo = YoloV3(classes=args.num_classes) yolo.load_weights(args.weights) logging.info('加载模型权重weights') # 加载目标类型 class_names = [c.strip() for c in open(args.classes).readlines()] # 2、加载图片处理图片并使用模型进行预测 img = tf.image.decode_image(open(args.image, 'rb').read(), channels=3) img = tf.expand_dims(img, 0) img = transform_images (img, args.size) # 记录时间 t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() logging.info('耗时: {}'.format(t2 - t1)) logging.info('检测结果:') print(boxes, scores, classes, nums) for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) # 3、显示图片并将图片框画出 img = cv2.imread(args.image) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(args.output, img) logging.info('output saved to: {}'.format(args.output))
def get_detections(): raw_images = [] images = os.listdir(os.getcwd() + '/uploads') image_names = [] for image in images: image_names.append(image) image_file = os.getcwd() + '/uploads/' + image img_raw = tf.image.decode_image( open(image_file, 'rb').read(), channels=3) raw_images.append(img_raw) num = 0 # create list for final response response = [] for j in range(len(raw_images)): # create list of responses for current image responses = [] raw_img = raw_images[j] num+=1 img = tf.expand_dims(raw_img, 0) img = transform_images(img, size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() print('time: {}'.format(t2 - t1)) print('detections:') for i in range(nums[0]): print('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) responses.append({ "class": class_names[int(classes[0][i])], "confidence": float("{0:.2f}".format(np.array(scores[0][i])*100)) }) response.append({ "image": image_names[j], "detections": responses }) img = cv2.cvtColor(raw_img.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(output_path + 'detection' + str(num) + '.jpg', img) print('output saved to: {}'.format(output_path + 'detection' + str(num) + '.jpg')) #remove temporary images # for name in image_names: # os.remove(name) try: new_list = [] for i in response[0]["detections"]: new_list.append(list(i.values())[0]) new_list = [i for i in new_list if i in allowable_items] new_dict = {} for i in new_list: new_dict[i] = get.get_walmart_data(i, 1) return (response[0], new_dict) except FileNotFoundError: return " "
def get_image(): image = request.files["images"] image_name = image.filename image.save(os.path.join(os.getcwd(), image_name)) img_raw = tf.image.decode_image(open(image_name, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() print('time: {}'.format(t2 - t1)) print('detections:') for i in range(nums[0]): print('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(output_path + 'detection.jpg', img) print('output saved to: {}'.format(output_path + 'detection.jpg')) # prepare image for response _, img_encoded = cv2.imencode('.png', img) response = img_encoded.tostring() #remove temporary image os.remove(image_name) try: return Response(response=response, status=200, mimetype='image/png') except FileNotFoundError: abort(404)
def frames(): cam = cv2.VideoCapture(r'./finish.mp4') if not cam.isOpened(): raise RuntimeError('Could not start camera.') while True: # read current frame _, img = cam.read() try: if CameraParams.gray: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if CameraParams.gaussian: img_raw = tf.convert_to_tensor(img) img_raw = tf.expand_dims(img_raw, 0) # img detect img_raw = transform_images(img_raw, size) boxes, scores, classes, nums = yolo(img_raw) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) if CameraParams.sobel: if (len(img.shape) == 3): img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) # x img = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) # y if CameraParams.canny: img = cv2.Canny(img, 100, 200, 3, L2gradient=True) except Exception as e: print(e) # encode as a jpeg image and return it yield cv2.imencode('.jpg', img)[1].tobytes()
def get_frame(self): font = ImageFont.load_default() # vid = cv2.VideoCapture(0) ret, frame = self.video.read() if frame is None: logging.warning("Empty Frame") logging.info('weights loaded') class_names = [c.strip() for c in open('coco.names').readlines()] logging.info('classes loaded') img = frame img_in = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_in = tf.expand_dims(img_in, 0) img_in = transform_images(img_in, 416) fps = 0.0 t1 = time.time() boxes, scores, classes, nums = self.yolo.predict(img_in) fps = (fps + (1. / (time.time() - t1))) / 2 img = draw_outputs(img, (boxes, scores, classes, nums), class_names) # img = cv2.putText(img, "FPS: {:.2f}".format(fps), (0, 30), f1, (0, 0, 255), 2) img = cv2.putText(img, "FPS: {:.2f}".format(fps), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) ret, jpeg = cv2.imencode('.jpg', img) return jpeg.tobytes()
def detect_image(img_raw, image_pre, ext, yolo, class_names): # 重构图片格式——416 img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) # 日志中的时间标注,当前时间、运行时间 t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('detections:') # 日志中将检测出的目标逐个标注出类别、分数、boxes for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) warning(nums, image_pre, class_names, classes, boxes) # 图像上画出检测出的目标,并标注相关信息,但是nums是什么? img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(FLAGS.output + image_pre + '_out' + ext, img) # 图像输出 logging.info('output saved to: {}'.format(FLAGS.output + image_pre + '_out' + ext)) return
def main(_argv): if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights) logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') img = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img, 0) img = transform_images(img, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('detections:') for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) img = cv2.imread(FLAGS.image) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(FLAGS.output, img) logging.info('output saved to: {}'.format(FLAGS.output))
def get_image(): if request.method == 'POST': raw_images =[] images = os.listdir(os.getcwd() + '/uploads') for image in images: for i in ["jpg", "jpeg", "png", "bmp"]: if image.endswith(i): image_file = os.getcwd() + '/uploads/' + image img_raw = tf.image.decode_image(open(image_file, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() print('time: {}'.format(t2 - t1)) print('detections') for i in range(nums[0]): print('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(output_path + 'detection.jpg', img) print('out saved to: {}'.format(output_path + 'detection.jpg')) # prepare image for response _, img_encoded = cv2.imencode('.png', img) response = img_encoded.tostring() try: return Response(response=response, status=200, mimetype='image/png') except FileNotFoundError: abort(404)
def display_detections(self): start = time.time() tick = 0 frame_counter = 0 while True: _, cv_img = self.cap.read() if cv_img is None: logging.warning("Empty Frame") time.sleep(0.1) continue img_tf = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB) img_tf = tf.expand_dims(img_tf, 0) img_tf = transform_images(img_tf, 416) boxes, scores, classes, nums = self.yolo.predict(img_tf) cv_img = draw_outputs(cv_img, (boxes, scores, classes, nums), self.class_names) # Display the detection cv2.imshow('output', cv_img) if cv2.waitKey(1) == ord('q'): break # Print FPS frame_counter += 1 time_now = time.time() - start if (time_now - tick >= 1): tick += 1 print(cv_img.shape) print("FPS:", frame_counter) frame_counter = 0
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') if len(physical_devices) > 0: tf.config.experimental.set_memory_growth(physical_devices[0], True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights).expect_partial() class_names = [c.strip() for c in open(FLAGS.classes).readlines()] if FLAGS.tfrecord: dataset = load_tfrecord_dataset(FLAGS.tfrecord, FLAGS.classes, FLAGS.size) dataset = dataset.shuffle(512) img_raw, _label = next(iter(dataset.take(1))) else: img_raw = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(FLAGS.output, img)
def main(_argv): if FLAGS.tiny: yolo = YoloV3Tiny() else: yolo = YoloV3() yolo.load_weights(FLAGS.weights) logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') times = [] while True: (rpiName, frame) = imageHub.recv_image() imageHub.send_reply(b'OK') img = imutils.resize(frame, width=416) img_in = tf.expand_dims(img, 0) img_in = transform_images(img_in, FLAGS.size) boxes, scores, classes, nums = yolo.predict(img_in) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) # img = cv2.putText(img, "Time: {:.2f}ms".format(sum(times)/len(times)*1000), (0, 30), # cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) cv2.imshow('output', img) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows()
def predict(img): arr = tf.expand_dims(img, 0) arr = transform_images(arr, FLAGS.size) FLAGS.yolo_score_threshold = 0.5 boxes, scores, classes, nums = yolo(arr) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) return img
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) model = tf.saved_model.load(FLAGS.model) infer = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY] logging.info('Model loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') times = [] try: vid = cv2.VideoCapture(int(FLAGS.video)) except: vid = cv2.VideoCapture(FLAGS.video) out = None if FLAGS.output: # by default VideoCapture returns float instead of int width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(vid.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*FLAGS.output_format) out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height)) while True: _, img = vid.read() if img is None: logging.warning("Empty Frame") time.sleep(0.1) continue img_in = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_in = tf.expand_dims(img_in, 0) img_in = transform_images(img_in, FLAGS.size) t1 = time.time() outputs = infer(img_in) boxes, scores, classes, nums = outputs["yolo_nms"], outputs[ "yolo_nms_1"], outputs["yolo_nms_2"], outputs["yolo_nms_3"] t2 = time.time() times.append(t2 - t1) times = times[-20:] img = draw_outputs(img, (boxes, scores, classes, nums), class_names) img = cv2.putText( img, "Time: {:.2f}ms".format(sum(times) / len(times) * 1000), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) if FLAGS.output: out.write(img) cv2.imshow('output', img) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows()
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') if len(physical_devices) > 0: tf.config.experimental.set_memory_growth(physical_devices[0], True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights) logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') times = [] vid = cv2.VideoCapture(0, cv2.CAP_DSHOW) # out = None if FLAGS.output: # by default VideoCapture returns float instead of int width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(vid.get(cv2.CAP_PROP_FPS)) # codec = cv2.VideoWriter_fourcc(*FLAGS.output_format) # out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height)) while True: _, img = vid.read() if img is None: logging.warning("Empty Frame") time.sleep(0.25) continue img_in = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_in = tf.expand_dims(img_in, 0) img_in = transform_images(img_in, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo.predict(img_in) t2 = time.time() times.append(t2 - t1) times = times[-20:] img = draw_outputs(img, (boxes, scores, classes, nums), class_names) img = cv2.putText( img, "Time: {:.2f}ms".format(sum(times) / len(times) * 1000), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) # if FLAGS.output: # out.write(img) cv2.imshow('output', img) if cv2.waitKey(1) == ord('q'): break cv2.destroyAllWindows()
def main(_argv): # Change flag values if FLAGS.height is None: FLAGS.height = FLAGS.size if FLAGS.width is None: FLAGS.width = FLAGS.size size = (FLAGS.height, FLAGS.width) physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights).expect_partial() logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') if FLAGS.tfrecord: dataset = load_tfrecord_dataset(FLAGS.tfrecord, FLAGS.classes, size) dataset = dataset.shuffle(512) img_iter = iter(dataset.take(FLAGS.count)) else: img_raw = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img_iter = iter([(img_raw, None)]) for idx, (img_raw, _label) in enumerate(img_iter): img = tf.expand_dims(img_raw, 0) img = transform_images(img, size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('detections:') for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) output = FLAGS.output if idx != 0: dot = output.rfind(".") suffix = "-" + str(idx).zfill(3) if dot != -1: output = output[:dot] + suffix + output[dot:] else: output = output + suffix cv2.imwrite(output, img) logging.info('output saved to: {}'.format(output))
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights).expect_partial() logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') if FLAGS.tfrecord: dataset = load_tfrecord_dataset(FLAGS.tfrecord, FLAGS.classes, FLAGS.size) dataset = dataset.shuffle(512) img_raw, _label = next(iter(dataset.take(1))) else: img_raw = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('primary detections:') for i in range(nums[0]): logging.info('\t{}, {:.2f}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]))) img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) t3 = time.time() img, cat_det, dog_det = draw_outputs(img, (boxes, scores, classes, nums), class_names) t4 = time.time() cv2.imwrite(FLAGS.output, img) if np.size(cat_det) != 0 or np.size(dog_det) != 0: logging.info('secondary detections :') logging.info('time: {}'.format(t4 - t3)) if np.size(cat_det) != 0: for cat in cat_det: logging.info('\t{}, {:.2f}'.format(cat[1], cat[0])) if np.size(dog_det) != 0: for dog in dog_det: logging.info('\t {}, {:.2f}'.format(dog[1], dog[0])) logging.info('output saved to: {}'.format(FLAGS.output)) cv2.imshow(FLAGS.output)
def run(self) -> None: print('开始线程') times = [] vid = cv2.VideoCapture(self.video) out = None if FLAGS.output: # by default VideoCapture returns float instead of int width = int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(vid.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*FLAGS.output_format) out = cv2.VideoWriter(FLAGS.output, codec, fps, (width, height)) print(width, height, fps, codec, out) # F的值代表每隔F帧计算一次目标 F = frame_count FF = F boxes = tf.zeros(shape=[1, 100, 4]) scores = tf.zeros(shape=[1, 100]) classes = tf.zeros(shape=[1, 100]) nums = tf.zeros(shape=[ 1, ]) last_flag = 0 while True: firstPic = False _, img = vid.read() if img is None: logging.warning("Empty Frame") time.sleep(0.1) continue img_in = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_in = tf.expand_dims(img_in, 0) img_in = transform_images(img_in, FLAGS.size) t1 = time.time() if FF % F == 0: FF = F firstPic = True boxes, scores, classes, nums = self.yolo.predict(img_in) # print(boxes.shape,scores.shape,classes.shape,nums.shape) t2 = time.time() times.append(t2 - t1) times = times[-20:] img = draw_outputs(firstPic, img, (boxes, scores, classes, nums), self.class_names) img = cv2.putText( img, "Time: {:.2f}ms".format(sum(times) / len(times) * 1000), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) # if FLAGS.output: # out.write(img) cv2.imshow(self.video, img) FF = FF + 1 if cv2.waitKey(1) == ord('q'): break
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) # Load weights yolo.load_weights(FLAGS.weights).expect_partial() logging.info('weights loaded') # Load classnames class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info(f'classes loaded: {class_names}') dataset = load_tfrecord_dataset(file_pattern=FLAGS.dataset, class_file=FLAGS.classes, size=FLAGS.size) dataset = dataset.as_numpy_iterator() times = [] counter = 0 for val_data in tqdm(dataset): counter += 1 file_name = f"image_size_{im_size}/tiny/score_threshold_{score_threshold}/image-{counter}.txt" image_name = f"image_size_{im_size}/score_threshold_{score_threshold}/images/image-{counter}.jpeg" img_raw, _label = val_data img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() times.append([np.subtract(t2, t1)]) logging.info('time: {}'.format(t2 - t1)) if FLAGS.save_images: img = cv2.cvtColor(img_raw, cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names, text=True, color=(0, 0, 255), obj=False) cv2.imwrite(os.path.join(DETECTIONS_DIR, image_name), img) else: open(os.path.join(DETECTIONS_DIR, file_name), "w").close() for i in range(nums[0]): left, top, right, bottom = np.array(boxes[0][i]) * FLAGS.size with open(os.path.join(DETECTIONS_DIR, file_name), 'a+') as f: f.write(f"{class_names[int(classes[0][i])]} " f"{np.array(scores[0][i])} " f"{round(Decimal(str(left)), 2)} " f"{round(Decimal(str(top)), 2)} " f"{round(Decimal(str(right)), 2)} " f"{round(Decimal(str(bottom)), 2)}\n") print(f"Mean detection time for dataset: {data_set_name} " f"with image size: {im_size} " f"and score threshold: {score_threshold} is: " f"{round(np.mean(times), 3)}, fps: {round(1/(np.mean(times)), 2)}")
def get_detections(): raw_images = [] images = request.files.getlist("images") image_names = [] for image in images: image_name = image.filename image_names.append(image_name) image.save(os.path.join(os.getcwd(), image_name)) img_raw = tf.image.decode_image(open(image_name, 'rb').read(), channels=3) raw_images.append(img_raw) num = 0 # create list for final response response = [] for j in range(len(raw_images)): # create list of responses for current image responses = [] raw_img = raw_images[j] num += 1 img = tf.expand_dims(raw_img, 0) img = transform_images(img, size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() print('time: {}'.format(t2 - t1)) print('detections:') for i in range(nums[0]): print('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) responses.append({ "class": class_names[int(classes[0][i])], "confidence": float("{0:.2f}".format(np.array(scores[0][i]) * 100)) }) response.append({"image": image_names[j], "detections": responses}) img = cv2.cvtColor(raw_img.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(output_path + 'detection' + str(num) + '.jpg', img) print('output saved to: {}'.format(output_path + 'detection' + str(num) + '.jpg')) print(json.dumps(response)) #remove temporary images # for name in image_names: # os.remove(name) # try: # return render_template("index.html", user_image = full_filename) # return jsonify({"response":response}), 200 # return response, 200 return Response(json.dumps(response), status=201, mimetype='application/json')
def analisisArma(id_srch): mypath = "./tmp/post_img/" + str(id_srch) #carpeta de imagenes descargadas por busqueda onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))] #lista de esas imagenes # carga imagen por imagen y la manda a analizar for img in onlyfiles: s = str(img).replace('.jpg', '') datos = s.split("-") idPerfil = str(datos[0]) idPublicacion = str(datos[1]) IMAGE_PATH = mypath + "/" + img image = open(IMAGE_PATH, "rb").read() #################---BLOQUE AÑDIDO PARA TEST ("detecciones/")-----################# raw_images = [] img_raw = tf.image.decode_image(image,channels=3) raw_images.append(img_raw) num = 0 for j in range(len(raw_images)): # se arma el resultado responses = [] raw_img = raw_images[j] num+=1 img = tf.expand_dims(raw_img, 0) img = transform_images(img, size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() for i in range(nums[0]): responses.append({ "arma": "SI", "porcentaje": float("{0:.2f}".format(np.array(scores[0][i])*100)) }) img = cv2.cvtColor(raw_img.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) final_path = DETECTIONS_PATH + str(id_srch) + "/"+idPerfil+"-" + idPublicacion + '.jpg' cv2.imwrite(final_path, img) #################:::::::::::::::::::::::::::################# data = responses #print(data) if not data: #print("no data") continue else: try: new_data = str(data).replace("\'", "\"") dataObj = json.loads(new_data) for obj in dataObj: if obj["arma"] == "SI": res = 1 insert_arma(idPublicacion,obj["porcentaje"],res) upd_val_arma(idPublicacion,"1") except Exception as e: print("ERROR al ejecutar la funcion insert_arma() :"+str(e)) return False return True
def save_image(raw_img, num, outputs, return_img=False): img = cv2.cvtColor(raw_img.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, outputs, class_names, unallowed_class_names) cv2.imwrite(OUTPUT_PATH + 'detection' + str(num) + '.jpg', img) print('output saved to: {}'.format(OUTPUT_PATH + 'detection' + str(num) + '.jpg')) if return_img: return img return None
def get_detections(): raw_images = [] images = request.files.getlist("images") #images = request.files.get('images') image_names = [] print('images') print(images) for image in images: image_name = image.filename image_names.append(image_name) image.save(os.path.join(os.getcwd(), image_name)) img_raw = tf.image.decode_image(open(image_name, 'rb').read(), channels=3) raw_images.append(img_raw) num = 0 # create list for final response response = [] for j in range(len(raw_images)): # create list of responses for current image responses = [] raw_img = raw_images[j] num += 1 img = tf.expand_dims(raw_img, 0) img = transform_images(img, size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() print('detections:') for i in range(nums[0]): print('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) responses.append({ "class": class_names[int(classes[0][i])], "confidence": float("{0:.2f}".format(np.array(scores[0][i]) * 100)) }) response.append({"image": image_names[j], "detections": responses}) img = cv2.cvtColor(raw_img.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) #remove temporary images for name in image_names: os.remove(name) try: print(response) return jsonify({"response": response}), 200 except FileNotFoundError: abort(404)
def main(_argv): physical_devices = tf.config.experimental.list_physical_devices('GPU') if len(physical_devices) > 0: tf.config.experimental.set_memory_growth(physical_devices[0], True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights) logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') nfp = 0 npp = 0 nxp = 0 files = os.listdir('data/scaled') for fn in files: lc = fn[len(fn) - 5] test_data = lc == '0' if not flags.FLAGS.all and not test_data: continue img = tf.image.decode_image(open('data/scaled/' + fn, 'rb').read(), channels=3) img = tf.expand_dims(img, 0) img = transform_images(img, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) logging.info('detections:') for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) nfp += 1 if nums[0] > 0: npp += 1 img = cv2.imread('data/scaled/' + fn) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) ofn = 'data/predict/' + fn if FLAGS.save or test_data: cv2.imwrite(ofn, img) logging.info('output saved to: {}'.format(ofn)) if nums[0] == 4: nxp += 1 print("%d processed. %d some prediction. %d has complete prediction\n" % (nfp, npp, nxp)) print("%1.0f %% pass prediction" % (100 * nxp / nfp))
def main(_argv): del _argv if FLAGS.tiny: yolo = YoloV3Tiny() else: yolo = YoloV3() # yolo.summary() yolo.load_weights(FLAGS.weights) logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') url = images_url.copy() img = url[0] img = tf.image.decode_image(open(img, 'rb').read(), channels=3) img = tf.expand_dims(img, 0) im = transform_images(img, 416) for pic in url[1:]: img1 = tf.image.decode_image(open(pic, 'rb').read(), channels=3) img1 = tf.expand_dims(img1, 0) img1 = transform_images(img1, 416) im = tf.concat((im, img1), axis=0) print(tf.shape(im)) t1 = time.time() Tboxes, Tscores, Tclasses, Tnums = yolo(im) t2 = time.time() logging.info('time: {}'.format(t2 - t1)) # boxes, scores, classes, nums=a # print(tf.shape(Tscores[0:1,:])) # print(tf.shape(Tclasses)) # print(tf.shape(Tboxes)) # print(tf.shape(Tnums[0:1])) for pic in range(tf.shape(Tnums)): scores = Tscores[0 + pic:1 + pic, :] classes = Tclasses[0 + pic:1 + pic, :] boxes = Tboxes[0 + pic:1 + pic, :, :] nums = Tnums[0 + pic:1 + pic] logging.info('detections:') print(nums[0]) for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], scores[0][i].numpy(), boxes[0][i].numpy())) img = cv2.imread(images_url[pic]) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(str(pic) + '.jpg', img) logging.info('output saved to: {}'.format('output' + str(pic)))
def main(_argv): id_img = 0 physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: yolo = YoloV3(classes=FLAGS.num_classes) yolo.load_weights(FLAGS.weights).expect_partial() logging.info('weights loaded') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') logging.info('initialization connexion at {}:{}'.format(FLAGS.ip, str(FLAGS.port))) connexion_1 = time.time() sk = nt.init_connexion(FLAGS.ip, FLAGS.port) connexion_2 = time.time() connexion = connexion_2 - connexion_1 logging.info("connected to {} port {} in {:.3f}ms".format(FLAGS.ip, FLAGS.port, connexion)) if FLAGS.tfrecord: dataset = load_tfrecord_dataset( FLAGS.tfrecord, FLAGS.classes, FLAGS.size) dataset = dataset.shuffle(512) img_raw, _label = next(iter(dataset.take(1))) else: img_raw = tf.image.decode_image( open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) nt.get_more_data(img, (boxes, scores, classes, nums), class_names) logging.info('time: {}'.format(t2 - t1)) logging.info('primary detections:') for i in range(nums[0]): logging.info('\t{}, {:.2f}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]))) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(FLAGS.output, img) logging.info('output saved to: {}'.format(FLAGS.output)) sk.close()
def main(args): class_path = args.classes # Path to classes file weights = args.weights # Path to weight file image_size = cfg.IMAGE_SIZE # Resize images to size - 416 04 608 image = '' # Path to input image tfrecord = args.dataset # tfrecord instead of image or None output = args.output # Path to output image num_classes = args.num_classes # Number of classes in model anchors = cfg.YOLO_ANCHORS anchor_masks = cfg.YOLO_ANCHOR_MASKS physical_devices = tf.config.experimental.list_physical_devices('GPU') for physical_device in physical_devices: tf.config.experimental.set_memory_growth(physical_device, True) yolo = YoloV3(image_size, training=False, classes=num_classes) yolo.load_weights(weights).expect_partial() print('weights loaded') class_names = [c.strip() for c in open(class_path).readlines()] print('classes loaded') if tfrecord: val_dataset = load_tfrecord_dataset(tfrecord, class_path, image_size) # val_dataset = val_dataset.shuffle(512) val_dataset = val_dataset.batch(1) val_dataset = val_dataset.map(lambda x, y: ( transform_images(x, image_size), transform_targets(y, anchors, anchor_masks, image_size))) # img_raw, _label = next(iter(dataset.take(1))) else: img_raw = tf.image.decode_image(open(image, 'rb').read(), channels=3) index = 0 for img_raw, _label in val_dataset.take(25): # img = tf.expand_dims(img_raw, 0) img = transform_images(img_raw, image_size) img = img * 255 boxes, scores, classes, nums = yolo(img) output = '/Users/justinbutler/Desktop/test/test_images/test_{}.jpg'.format( index) output = '/home/justin/Models/yolov3-tf2/test_images/test_{}.jpg'.format( index) print('output saved to: {}'.format(output)) img = cv2.cvtColor(img_raw[0].numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names, thresh=0) img = img * 255 cv2.imwrite(output, img) index = index + 1
def main(_argv): global g_yolo for i in (1, 2): S = time.time() physical_devices = tf.config.experimental.list_physical_devices('GPU') if len(physical_devices) > 0: tf.config.experimental.set_memory_growth(physical_devices[0], True) t1 = time.time() if FLAGS.tiny: yolo = YoloV3Tiny(classes=FLAGS.num_classes) else: if g_yolo is not None: yolo = g_yolo else: yolo = YoloV3(classes=FLAGS.num_classes) g_yolo = yolo yolo.load_weights(FLAGS.weights).expect_partial() t2 = time.time() logging.info('weights loaded') logging.info(f'graph_construction_time_{i} {t2-t1}') class_names = [c.strip() for c in open(FLAGS.classes).readlines()] logging.info('classes loaded') if FLAGS.tfrecord: dataset = load_tfrecord_dataset(FLAGS.tfrecord, FLAGS.classes, FLAGS.size) dataset = dataset.shuffle(512) img_raw, _label = next(iter(dataset.take(1))) else: img_raw = tf.image.decode_image(open(FLAGS.image, 'rb').read(), channels=3) img = tf.expand_dims(img_raw, 0) img = transform_images(img, FLAGS.size) t1 = time.time() boxes, scores, classes, nums = yolo(img) t2 = time.time() logging.info('inference_time_{} {}'.format(i, t2 - t1)) logging.info('detections:') for i in range(nums[0]): logging.info('\t{}, {}, {}'.format(class_names[int(classes[0][i])], np.array(scores[0][i]), np.array(boxes[0][i]))) img = cv2.cvtColor(img_raw.numpy(), cv2.COLOR_RGB2BGR) img = draw_outputs(img, (boxes, scores, classes, nums), class_names) cv2.imwrite(FLAGS.output, img) logging.info('output saved to: {}'.format(FLAGS.output)) E = time.time() logging.info(f'phase_{i}_time: {E-S}')