def __init__(self, config_file="cfg/yolov4-leaky-416.cfg", weights="yolov4-leaky-416.weights", data_file='./cfg/coco.data', darknet_path=os.environ.get('DARKNET_PATH', '../../darknet/'), thresh=0.4, queueSize=128, debug=False): self.init_done = False self.stopped = True self.debug = debug self.network, self.class_names, self.class_colors = darknet.load_network( utils.join_strings_as_path([darknet_path, config_file]), utils.join_strings_as_path([darknet_path, data_file]), utils.join_strings_as_path([darknet_path, weights]), batch_size=1) self.search_box = DARKNET['search_box'] self.width = darknet.network_width(self.network) self.height = darknet.network_height(self.network) self.darknet_image = darknet.make_image(self.width, self.height, 3) self.thresh = thresh self.Q = object_detection_queue self.is_night_vision = None self.org_frame = None self.resize_frame = None self.dirs = utils.DirsHandler(DIRS) self.small_imgs = [] self.first_result = None self.init_done = True
def __init__(self, config, data_file, weights, batch_size): self.network, self.class_names, self.class_colors = darknet.load_network( config, data_file, weights, batch_size=batch_size )
def load_model(): origin_path = os.getcwd() path = os.environ['DARKNET_PATH'] + '/' os.chdir(path) # cd darknet net, cls, colors = dn.load_network("cfg/yolov4.cfg", "custom/class.data", "backup/v4/yolov4_best.weights") print(net, cls) print("done") os.chdir(origin_path) # cd .. return [net, cls, colors]
def __init__(self, id, batch_size = 15, dataset_dir='../others/dds/dataset/WildTrack/src/C'): # id: string self.id = id # video batch size integer self.batch_size = batch_size # displacement self.displacement_check = {} # Deep SORT encoding. setting is the same for now self.max_cosine_distance = 0.3 self.nn_budget = None self.nms_max_overlap = 1.0 self.temp_dir = 'temp-cropped' os.makedirs(self.temp_dir, exist_ok = True) self.dataset_dir = dataset_dir + id # read the total number of file from the server fnames = sorted(os.listdir(dataset_dir + id)) self.total_frame = len(fnames) print("Total number of frames: ", str(self.total_frame)) print("Simulating the camera with video frame size 15") # initiate the yolo v4 network network, class_names, class_colors = darknet.load_network( './darknet/cfg/yolov4.cfg', './darknet/cfg/coco.data', './darknet/yolov4.weights', batch_size=1 ) self.network = network self.class_names = class_names # initiate the deep sort network # multi-person tracking model_filename = 'model_data/mars-small128.pb' self.encoder = gdet.create_box_encoder(model_filename, batch_size=1) self.metric = nn_matching.NearestNeighborDistanceMetric("cosine", self.max_cosine_distance, self.nn_budget) self.tracker = Tracker(self.metric) print("Camera initiated")
def detect(): results = {} images = {} network, class_names, class_colors = darknet.load_network( CONFIG_FILE_PATH, DATA_FILE_PATH, WEIGHT_FILE_PATH, YOLO_BATCH_SIZE ) try: print('Image recognition started!') while True: # cv2.waitKey(50) direction, x_coordinate , y_coordinate = 0, 0, 0 frame = retrieveImg() image, detections = imageDetection(frame, network, class_names, class_colors, THRESH) #structure: list #element structure: (id, confidence, (bbox)) #bbox: x, y, w, h for i in detections: id = i[0] confidence = i[1] bbox = i[2] #Code for evaluating bbox area print('ID detected: ' + id, ', confidence: ' + confidence) print('Bounding box: x = ' + str(bbox[0]) + ' y = ' + str(bbox[1]) + ' Width = ' + str(bbox[2]) + ' Height =' + str(bbox[3])) dist_est(bbox[3], id, direction, x_coordinate, y_coordinate, int(bbox[0])) if id in symb_confidence: if id in results: print('ID has been detected before') # if float(confidence) > float(results[id][1]): if float(confidence) > float(results[id][0][1]) and max(symb_confidence[id].keys()) >= max(results[id][1].keys()): print('Higher confidence. Replacing existing image.') # Removes existing result from dict del results[id] # Removes existing img from dict del images[id] # Adds new result to dict # results[id] = i # results[id] = [i, symb_confidence[id][max(symb_confidence[id].keys())]] results[id] = [i, symb_confidence[id]] # Adds new result to dict images[id] = image # saveImage(image, id) # sendAndroidString(results) else: print('Lower confidence. Keeping existing image.') pass else: print('New ID. Saving to results and images dict.') # results[id] = i # results[id] = [i, symb_confidence[id][max(symb_confidence[id].keys())]] results[id] = [i, symb_confidence[id]] images[id] = image # saveImage(image, id) # sendAndroidString(results) except KeyboardInterrupt: print('Image recognition ended') result_string = '{' print("Results:") for id in results: x_coordinate , y_coordinate = results[id][1][max(symb_confidence[id].keys())][0], results[id][1][max(symb_confidence[id].keys())][1] id_coordinate_str = '(' + id + ',' + str(x_coordinate) + ',' + str(y_coordinate) + '),' result_string += id_coordinate_str print('ID: ' + id + ', Coordinates: (' + str(x_coordinate) +',' + str(y_coordinate) + ')' + ', Confidence: ' + results[id][1]) if result_string[-1] == ',': result_string = result_string[:-1] result_string += '}' print(result_string) # android_full_string = sendAndroidString(results) # print('Sent ' + android_full_string + ' to Android.') #generate image mosaic result_list = list(images.values()) showImages(result_list)
""" import os import json from bedrock_client.bedrock.metrics.service import ModelMonitoringService from flask import Flask, Response, current_app, request import cv2 from PIL import Image import darknet.darknet as darknet DATA_FOLDER = os.environ.get("DATA_FOLDER") YOLO_VARS = darknet.load_network( config_file="darknet/cfg/yolov4-tiny-custom.cfg", data_file=f"./darknet/{DATA_FOLDER}/obj.data", weights="/artefact/yolov4-tiny-custom_latest.weights", batch_size=1, ) def image_detection(image_path, network, class_names, class_colors, thresh): """Run YOLOv4 models.""" # Darknet doesn't accept numpy images. # Create one with image we reuse for each detect width = darknet.network_width(network) height = darknet.network_height(network) darknet_image = darknet.make_image(width, height, 3) image = cv2.imread(image_path) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height),
def detect(): results = {} images = {} network, class_names, class_colors = darknet.load_network( CONFIG_FILE_PATH, DATA_FILE_PATH, WEIGHT_FILE_PATH, YOLO_BATCH_SIZE) try: # if (run): print('Image recognition started!') prev_dir, prev_x, prev_y = -1, -1, -1 direction, x_coordinate, y_coordinate = -1, -1, -1 while len(results) < 5 or (confidenceBool and checkConfidence()): # while len(results) < 5: direction, x_coordinate, y_coordinate = getPosition() if direction == -1: break if direction != prev_dir or x_coordinate != prev_x or y_coordinate != prev_y: time.sleep(0.05) frame = retrieveImg() image, detections = imageDetection(frame, network, class_names, class_colors, THRESH) #structure: list #element structure: (id, confidence, (bbox)) #bbox: x, y, w, h for i in detections: id = i[0] confidence = i[1] bbox = i[2] #Prototype: Skip side images using condition on width / height ratio if float(bbox[2]) / float(bbox[3]) < 0.6: continue print('ID detected: ' + id, ', confidence: ' + confidence) #Code for evaluating bbox area # print('Bounding box: Width = ' + str(bbox[2]) + ' Height =' + str(bbox[3])) dist_est(bbox[3], id, direction, x_coordinate, y_coordinate, round(float(bbox[0]), 1)) if id in symb_confidence: if id in results: print('ID has been detected before') # if float(confidence) > float(results[id][1]): #if float(confidence) > float(results[id][0][1]) and max(symb_confidence[id].keys()) >= max(results[id][1].keys()): if float(confidence) > 98.0 and max( symb_confidence[id].keys()) >= max( results[id][1].keys()): print( 'Higher confidence. Replacing existing image.' ) # Removes existing result from dict del results[id] # Removes existing img from dict del images[id] # Adds new result to dict # results[id] = i # results[id] = [i, symb_confidence[id][max(symb_confidence[id].keys())]] results[id] = [i, symb_confidence[id]] # Adds new result to dict images[id] = image saveImage(image, id) sendAndroidString(results) else: print( 'Lower confidence. Keeping existing image.' ) pass else: print('New ID. Saving to results and images dict.') # results[id] = i # results[id] = [i, symb_confidence[id][max(symb_confidence[id].keys())]] results[id] = [i, symb_confidence[id]] images[id] = image saveImage(image, id) sendAndroidString(results) prev_dir, prev_x, prev_y = direction, x_coordinate, y_coordinate # print(results) # else: # raise Exception except KeyboardInterrupt: print('Image recognition ended') result_string = '{' print("Results:") for id in results: image = results[id] x_coordinate, y_coordinate = image[1][max( image[1].keys())][0], image[1][max(image[1].keys())][1] id_coordinate_str = '(' + id + ',' + str(x_coordinate) + ',' + str( y_coordinate) + '),' result_string += id_coordinate_str print('ID: ' + id + ', Coordinates: (' + str(x_coordinate) + ',' + str(y_coordinate) + ')' + ', Confidence: ' + str(image[0][1])) if result_string[-1] == ',': result_string = result_string[:-1] result_string += '}' print(result_string) android_full_string = sendAndroidString(results) print('IMG - Sent to Android:' + android_full_string) ir_socket.send("STOP".encode(FORMAT)) # ir_socket.send("AR-T".encode(FORMAT)) showFinalImage(SAVED_IMAGE_PATH) # result_list = list(images.values()) # showImages(result_list) return