def main(): model = YOLOv3Net(cfgfile, model_size, num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) image = cv2.imread(img_path) image = np.array(image) image = tf.expand_dims(image, 0) resized_frame = resize_image(image, (model_size[0], model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) image = np.squeeze(image) img = draw_outputs(image, boxes, scores, classes, nums, class_names) win_name = 'Image detection' cv2.imshow(win_name, img) cv2.waitKey(0) cv2.destroyAllWindows()
def main(): model = YOLOv3Net(cfgfile, model_size, num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) win_name = 'Yolov3 detection' cv2.namedWindow(win_name) #specify the vidoe input. # 0 means input from cam 0. # For vidio, just change the 0 to video path cap = cv2.VideoCapture(0) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) try: while True: start = time.time() ret, frame = cap.read() if not ret: break resized_frame = tf.expand_dims(frame, 0) resized_frame = resize_image(resized_frame, (model_size[0], model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) img = draw_outputs(frame, boxes, scores, classes, nums, class_names) cv2.imshow(win_name, img) stop = time.time() seconds = stop - start # print("Time taken : {0} seconds".format(seconds)) # Calculate frames per second fps = 1 / seconds print("Estimated frames per second : {0}".format(fps)) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break finally: cv2.destroyAllWindows() cap.release() print('Detections have been performed successfully.')
def get_prediction(inputimage): model = YOLOv3Net(cfgfile, model_size, num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) win_name = 'Yolov3 detection' cv2.namedWindow(win_name) #specify the vidoe input. # 0 means input from cam 0. # For vidio, just change the 0 to video path frame = cv2.imread(inputimage, 1) frame_size = frame.shape try: # Read frame resized_frame = tf.expand_dims(frame, 0) resized_frame = resize_image(resized_frame, (model_size[0], model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) img = draw_outputs(frame, boxes, scores, classes, nums, class_names) cv2.imshow(win_name, img) cv2.imwrite('outputimgage.jpg', img) # print("Time taken : {0} seconds".format(seconds)) # Calculate frames per second finally: cv2.waitKey() cv2.destroyAllWindows() print('Detections have been performed successfully.') return img
def main(img_path, image_name): model = YOLOv3Net(cfgfile,model_size,num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) image = cv2.imread(os.path.join(img_path, "{}.jpg".format(image_name))) image = np.array(image) image = tf.expand_dims(image, 0) resized_frame = resize_image(image, (model_size[0],model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) image = np.squeeze(image) img = draw_outputs(image, boxes, scores, classes, nums, class_names) # win_name = 'Image detection' # cv2.imshow(win_name, img) # time.sleep(20) # cv2.destroyAllWindows() #If you want to save the result, uncommnent the line below: os.path.join(img_path, 'image_yolo.jpg') cv2.imwrite(os.path.join(img_path, "{}_yolo.jpg".format(image_name)), img)
def main(): weightfile = "weights/yolov3.weights" cfgfile = "cfg/yolov3.cfg" model_size = (416, 416, 3) num_classes = 80 model = YOLOv3Net(cfgfile, model_size, num_classes) load_weights(model, cfgfile, weightfile) try: model.save_weights('weights/yolov3_weights.tf') print('\nThe file \'yolov3_weights.tf\' has been saved successfully.') except IOError: print("Couldn't write the file \'yolov3_weights.tf\'.")
def main(): weightfile = "weights/yolov3.weights" cfgfile = "cfg/yolov3.cfg" model_size = (416, 416, 3) num_classes = 80 model = YOLOv3Net(cfgfile, model_size, num_classes) load_weights(model, cfgfile, weightfile) try: model.save_weights('weights/yolov3_weights.tf') print('\nFile is saved in tensor format') except IOError: print(" caouldn't write file to tensor")
def main(): weightfile = "weights/yolov3.weights" cfgfile = "cfg/yolov3_cfg.txt" model_size = (416, 416, 3) num_classes = 80 model = YOLOv3Net(cfgfile, model_size, num_classes) load_weights(model, cfgfile, weightfile) try: model.save_weights("weights/yolov3_weights.tf") print('\'yolov3_weights.tf\' has been saved') except IOError: print("Unable to write to \'yolov3_weights.tf\'")
def main(): model = YOLOv3Net(cfgfile, model_size, num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) print("class_names", class_names) image = cv2.imread(img_path) image = np.array(image) image = tf.expand_dims(image, 0) resized_frame = resize_image(image, (model_size[0], model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) image = np.squeeze(image) img = draw_outputs(image, boxes, scores, classes, nums, class_names) cv2.imwrite('result1.jpg', img)
def detect_image(img_path): model = YOLOv3Net(cfg.CFGFILE,cfg.MODEL_SIZE,cfg.NUM_CLASSES) model.load_weights(cfg.WEIGHTFILE) class_names = load_class_names(cfg.CLASS_NAME) image = cv2.imread(img_path) image = np.array(image) image = tf.expand_dims(image, 0) resized_frame = resize_image(image, (cfg.MODEL_SIZE[0],cfg.MODEL_SIZE[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, cfg.MODEL_SIZE, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=cfg.IOU_THRESHOLD, confidence_threshold=cfg.CONFIDENCE_THRESHOLD) image = np.squeeze(image) img = draw_outputs(image, boxes, scores, classes, nums, class_names) win_name = 'Detection' cv2.imshow(win_name, img) cv2.waitKey(0) cv2.destroyAllWindows()
def detect_video(video_path): model = YOLOv3Net(cfg.CFGFILE, cfg.MODEL_SIZE, cfg.NUM_CLASSES) model.load_weights(cfg.WEIGHTFILE) class_names = load_class_names(cfg.CLASS_NAME) win_name = 'Detection' cv2.namedWindow(win_name) cap = cv2.VideoCapture(returnCameraOrFile(video_path)) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) try: while True: start = time.time() ret, frame = cap.read() if not ret: break resized_frame = tf.expand_dims(frame, 0) resized_frame = resize_image( resized_frame, (cfg.MODEL_SIZE[0], cfg.MODEL_SIZE[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, cfg.MODEL_SIZE, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=cfg.IOU_THRESHOLD, confidence_threshold=cfg.CONFIDENCE_THRESHOLD) img = draw_outputs(frame, boxes, scores, classes, nums, class_names) cv2.imshow(win_name, img) stop = time.time() seconds = stop - start # Calculate frames per second fps = 1 / seconds print("Frames per second : {0}".format(fps)) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break finally: cv2.destroyAllWindows() cap.release() print('Detections performed successfully.')
def main(): model = YOLOv3Net(cfgfile, model_size, num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) image = cv2.imread(img_filename) image = np.array(image) image = tf.expand_dims(image, 0) resized_frame = resize_image(image, (model_size[0], model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) print('boxes', boxes) print('scores', scores[scores >= confidence_threshold]) print('classes', classes[classes != 0]) print('nums', nums) return 0 image = np.squeeze(image) img = draw_outputs(image, boxes, scores, classes, nums, class_names) # win_name = 'Image detection' # cv2.imshow(win_name, img) # cv2.waitKey(0) # cv2.destroyAllWindows() #If you want to save the result, uncommnent the line below: cv2.imwrite('data/images/output_dog.jpg', img)
def main(): # Kreiranje modela model = YOLOv3Net(cfgfile, model_size, num_classes) # Učitavanje istreniranih koeficijenata u model model.load_weights(weightfile) # Učitavanje imena klasa class_names = load_class_names(class_name) # Učitavanje ulaznih fotografija i predobrada u format koji očekuje model images_left = [] resized_images_left = [] filenames_left = [] # Load left camera data [images_left, resized_images_left, filenames_left] = loadAndResize(img_path_left_cam) images_right = [] resized_images_right = [] filenames_right = [] # Load right camera data [images_right, resized_images_right, filenames_right] = loadAndResize(img_path_right_cam) # Object distance and bounding box index distanceIndexPair = [] # Inferencija nad ulaznom slikom # izlazne predikcije pred - skup vektora (10647), gde svaki odgovara jednom okviru lokacije objekta for i in range(0, len(filenames_left)): resized_image = [] image = images_left[i] resized_image.append(resized_images_left[i]) resized_image.append(resized_images_right[i]) resized_image = tf.expand_dims(resized_image, 0) resized_image = np.squeeze(resized_image) pred = model.predict(resized_image) # Određivanje okvira oko detektovanih objekata (za određene pragove) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) # calculate distance distanceIndexPair = objectDistance(images_left[i], images_right[i], boxes, nums, classes) out_img = draw_outputs(image, boxes, scores, classes, nums, class_names, cLeftCamId, distanceIndexPair) # Čuvanje rezultata u datoteku out_file_name = './out/Izlazna slika.png' cv2.imwrite(out_file_name, out_img) # Prikaz rezultata na ekran cv2.imshow(out_file_name, out_img) #cv2.waitKey(0) if(cv2.waitKey(20) & 0xFF == ord('q')): cv2.destroyAllWindows() break
def main(): model = YOLOv3Net(cfgfile, model_size, num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) win_name = 'Yolov3 detection' cv2.namedWindow(win_name) # Specify the camera url. # For camera, just change the camera URL to match your IP camera RTSP stream or MPEG stream. cap = cv2.VideoCapture( "rtsp://*****:*****@172.168.50.208:554/cam/realmonitor?channel=1&subtype=1" ) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) try: while True: start = time.time() cap.grab() # Grab the most recent frame from the camera stream ret, frame = cap.read() # Read it into a frame buffer if not ret: break resized_frame = tf.expand_dims(frame, 0) resized_frame = resize_image(resized_frame, (model_size[0], model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( \ pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) img = draw_outputs(frame, boxes, scores, classes, nums, class_names) cv2.imshow(win_name, img) stop = time.time() seconds = stop - start # print("Time taken : {0} seconds".format(seconds)) # Calculate frames per second fps = 1 / seconds print("Estimated frames per second : {0}".format(fps)) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break if key == 27: break # Adjust frame rate #if fps > 30: # fps = fps * 0.5 # cap.set(cv2.CAP_PROP_FPS, int(fps)) # print("Changing frame rate to: {0}".format(int(fps))) #else: # cap.set(cv2.CAP_PROP_FPS, 10) # print("Changing frame rate to: {0}".format(int(fps))) finally: cv2.destroyAllWindows() cap.release() print('Detections have been performed successfully.')
class_name = './data/coco.names' max_output_size = 40 max_output_size_per_class= 20 iou_threshold = 0.5 confidence_threshold = 0.5 cfgfile = 'cfg/yolov3.cfg' weightfile = 'weights/yolov3_weights.tf' img_path = "data/images/person.jpg" model = YOLOv3Net(cfgfile,model_size,num_classes) model.load_weights(weightfile) # class_names = load_class_names(class_name) app = Flask(__name__) @app.route('/') def index(): return "<h1>The Server Works</h1>" @app.route('/upload', methods=['POST']) @cross_origin() def upload_base64_file(): """
def main(): model = YOLOv3Net(cfgfile, model_size, num_classes) model.load_weights(weightfile) class_names = load_class_names(class_name) win_name = 'Yolov3 detection' cv2.namedWindow(win_name) #To read from camera. #cap = cv2.VideoCapture(0) #To read a video file. videopath = 'data/videos/test.mp4' cap = cv2.VideoCapture(videopath) frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) try: i = 0 while True: start = time.time() ret, frame = cap.read() #print(ret) if not ret: break resized_frame = tf.expand_dims(frame, 0) resized_frame = resize_image(resized_frame, (model_size[0], model_size[1])) pred = model.predict(resized_frame) boxes, scores, classes, nums = output_boxes( pred, model_size, max_output_size=max_output_size, max_output_size_per_class=max_output_size_per_class, iou_threshold=iou_threshold, confidence_threshold=confidence_threshold) img = draw_outputs(frame, boxes, scores, classes, nums, class_names) cv2.imshow(win_name, img) frame_dir = 'output/frames/frame_%d.jpg' % i cv2.imwrite(frame_dir, img) i += 1 stop = time.time() elapsed_time = stop - start fps = int(1 / elapsed_time) print("estimated fps : %d" % fps) key = cv2.waitKey(1) if key == ord('q'): break finally: cv2.destroyAllWindows() cap.release() print('Detection perfomed successfully.')