def test_image(): r = requests.get( "https://raw.githubusercontent.com/madhawav/darknet/master/data/dog.jpg" ) assert r.status_code == 200 img = PIL.Image.open(BytesIO(r.content)) img = np.array(img) img = img[:, :, ::-1] # RGB to BGR net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) img2 = Image(img) results = net.detect(img2) print(results) results_labels = [x[0].decode("utf-8") for x in results] print(results_labels) assert "bicycle" in results_labels assert "dog" in results_labels assert "truck" in results_labels assert len(results_labels) == 3
def feature_predict(self, imgpath): net = Detector( bytes("custom/yolov3-tiny.cfg", encoding="utf-8"), bytes("custom/yolov3-tiny_400.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) multiplier_array = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] print(multiplier_array) img = cv2.imread("static/img/" + imgpath, cv2.COLOR_BGR2RGB) img2 = Image(img) results = net.detect(img2, 0.4) if "Black Spots on Orange" in str(results): print("Black Spots found in input image, creating bias for class") multiplier_array[4] = 1.5 if "Top White Fringe" in str(results): print( "Top White Fringe found in input image, creating bias for class" ) multiplier_array[4] = 1.5 for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) cv2.imwrite('static/features/' + imgpath, img) return multiplier_array
def __init__(self, type, image_set): super().__init__(type, image_set) self.allclasses = [] self.gridsize = 10 if (type is FeatureType.YOLO_NUM): with open("data/coco.names", 'r') as infile: for line in infile: self.allclasses.append(line.rstrip()) from pydarknet import Detector self.net = Detector( bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) elif (type is FeatureType.YOLO_NUM_9000 or type is FeatureType.YOLO_COMPOSITION): with open("data/9k.names", 'r') as infile: for line in infile: self.allclasses.append(line.rstrip()) from pydarknet import Detector import pydarknet #pydarknet.set_cuda_device(1) self.net = Detector( bytes("cfg/yolo9000.cfg", encoding="utf-8"), bytes("weights/yolo9000.weights", encoding="utf-8"), 0, bytes("cfg/combine9k.data", encoding="utf-8"))
def yolo_plants(img): net = Detector( bytes("cfg.taichun/yolov3-tiny.cfg", encoding="utf-8"), bytes("cfg.taichun/weights/yolov3-tiny_3600.weights", encoding="utf-8"), 0, bytes("cfg.taichun/obj.data", encoding="utf-8")) img2 = Image(img) results = net.detect(img2) for cat, score, bounds in results: cat = cat.decode("utf-8") if (cat == "Pteris_cretica"): boundcolor = (0, 238, 252) elif (cat == "Echeveria_Minibelle"): boundcolor = (227, 252, 2) elif (cat == "Crassula_capitella"): boundcolor = (249, 77, 190) x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) boundbox = cv2.imread("images/" + cat + ".jpg") print("read:", "images/" + cat + ".jpg") print(y, boundbox.shape[0], x, boundbox.shape[1]) #img[ int(y-h/2):int(y-h/2)+boundbox.shape[0], int(x-w/2):int(x-w/2)+boundbox.shape[1]] = boundbox img[int(y):int(y + boundbox.shape[0]), int(x):int(x + boundbox.shape[1])] = boundbox return img
def __init__(self, steer): # 저장된 YOLO 모델을 호출함 self.steer = steer self.net = Detector( bytes("YOLOv3/cfg/yolo-obj.cfg", encoding="utf-8"), bytes("YOLOv3/cfg/yolo-obj_400.weights", encoding="utf-8"), 0, bytes("YOLOv3/cfg/obj.data", encoding="utf-8"))
def preprocessImages(self, imagelist): net = Detector(bytes(cfgpath, encoding="utf-8"), bytes(weightpath, encoding="utf-8"), 0, bytes(objpath, encoding="utf-8")) fins = [] flukes = [] #process imagewise for i in imagelist: img = cv2.imread(i) img_darknet = Image(img) results = net.detect(img_darknet) #go through each result for the image for cat, score, bounds in results: label = str(cat).split("'")[1] #clean the label x, y, w, h = bounds #define the cropped area y1 = max(int(y - h / 2), 0) y2 = min(int(y + h / 2), img.shape[0]) x1 = max(int(x - w / 2), 0) x2 = min(int(x + w / 2), img.shape[1]) if label == "fin": fins.append([i, [x1, y1, x2, y2]]) elif label == "fluke": flukes.append([i, [x1, y1, x2, y2]]) return (fins, flukes)
def test_image(): assert r.status_code == 200 img = PIL.Image.open('/home/nvidia/Desktop/download.jpg') img = np.array(img) img = img[:, :, ::-1] # RGB to BGR cfg = bytes( "/home/nvidia/Documents/yolov3-tiny-tennis/yolov3-tiny-tennis.cfg", encoding="utf-8") weights = bytes( "/home/nvidia/Documents/yolov3-tiny-tennis/yolov3-tiny-tennis_last.weights", encoding="utf-8") data = bytes("/home/nvidia/Documents/darknet/data/obj.data", encoding="utf-8") net = Detector(cfg, weights, 0, data) img2 = Image(img) results = net.detect(img2) print(results) results_labels = [x[0].decode("utf-8") for x in results]
def camera_test(): net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) print("Attempting to capture webcam") cap = cv2.VideoCapture(0) print("Got capture") while True: r, frame = cap.read() if r: start_time = time.time() # Only measure the time taken by YOLO and API Call overhead dark_frame = Image(frame) results = net.detect(dark_frame) del dark_frame end_time = time.time() print("Elapsed Time:", end_time - start_time) for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(frame, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cv2.putText(frame, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 0)) cv2.imshow("preview", frame) k = cv2.waitKey(1) if k == 0xFF & ord("q"): break
def detect(cfg, weights, coco_data, source_video): print('Detecting vehicles (This may take time)...') net = Detector(bytes(cfg, encoding="utf-8"), bytes(weights, encoding="utf-8"), 0, bytes(coco_data,encoding="utf-8")) detections = [] i = 0 cap = cv2.VideoCapture(source_video) while True: ret, frame = cap.read() if not ret: break img = Image(frame) results = net.detect(img) if i%500==0: print(i) i+=1 detections.append(results) pkl.dump(detections, open('detections.pkl', 'wb'))
def __init__(self, monitor_zones, hostname): super(YoloAnalyzer, self).__init__(monitor_zones, hostname) self._ensure_configs() logger.info('Instantiating YOLO3 Detector...') with suppress_stdout_stderr(): self._net = Detector( bytes(self._config_path("yolov3.cfg"), encoding="utf-8"), bytes(self._config_path("yolov3.weights"), encoding="utf-8"), 0, bytes(self._config_path("coco.data"), encoding="utf-8")) logger.debug('Done instantiating YOLO3 Detector.')
def yoloDetect(img): # net = Detector(bytes("cfg/densenet201.cfg", encoding="utf-8"), bytes("densenet201.weights", encoding="utf-8"), 0, bytes("cfg/imagenet1k.data",encoding="utf-8")) net = Detector(bytes("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/test.cfg", encoding="utf-8"), bytes("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/backup/1208_1/yolo-obj_class4_20000.weights", encoding="utf-8"), 0, bytes("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/jingle.data",encoding="utf-8")) #img = cv2.imread(os.path.join(os.environ["DARKNET_HOME"],"data/dog.jpg")) #img = cv2.imread("/home/sfc_kamata/work/SFC_kamata/darknet_update/cfg/jingle/img/1.jpg") img2 = Image(img) # r = net.classify(img2) results = net.detect(img2) #writeBoundingBox(results) return results
def __init__(self, steer): self.steer = steer # Detection 및 Classification에 사용할 모델 구조(cfg), 학습한 가중치 결과(weights), label 분류 파일(data) 파일을 # Detector의 매개변수로 지정한다. self.net = Detector(bytes("./yolo/cfg/yolo-obj.cfg", encoding="utf-8"), bytes("./yolo/cfg/yolo-obj_400.weights", encoding="utf-8"), 0, bytes("./yolo/obj.data", encoding="utf-8"))
def __init__(self): root = '/app' configPath = root + '/brain/yolov3.cfg' weightsPath = root + '/brain/yolov3.weights' classesPath = root + '/brain/coco.data' self.net = Detector(bytes(configPath, encoding="utf-8"), bytes(weightsPath, encoding="utf-8"), 0, bytes(classesPath, encoding="utf-8"))
def __init__(self): self.options = {"model": "cfg/yolov3.cfg", "load": "weights/yolov3.weights", "threshold": 0.1, "gpu":0.9} self.net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data",encoding="utf-8")) self.json_results = {} self.l = [] #l = [vehicals(None,None,None,None)] self.NUMBER_VEHICLE = 1 self.uniquename = None self.data = defaultdict(lambda: defaultdict(list)) # bhavya: will store bbox for every id for every frame # self.data[frame_no][id] gives [x,y,w,h] self.data_rev = defaultdict(lambda: defaultdict(list)) # bhavya: data but with keys inverted
def recognize(file): #Read img #img = cv2.imread(os.path.join(os.environ["DARKNET_HOME"], filename)) img = cv2.imread(file) dark_img = Image(img) print("Filename: ", file) #create darknet detector net = Detector(bytes("cfg/yolofinal.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/data.data", encoding="utf-8")) res = [] start_time = time.time() #Detect! can be given threshold parameters - see top of file :) #Has a standard threshold value of .5 results = net.detect(dark_img) end_time = time.time() if len(results) == 0: print("No results") else: print("There are Results") # print(results) print("Elapsed Time:", end_time - start_time) for cat, score, bounds in results: x, y, w, h = bounds #bytecode-decode to string cat_str = cat.decode("utf-8") #Format JSON json_obj = { "x": x, "y": y, "w": w, "h": h, "cat": cat_str, "score": score } #Append JSON to list res.append(json_obj) cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_DUPLEX, 4, (0, 0, 255), thickness=2) print(res) cv2.imwrite(os.path.join("output", "outputpic.png"), img) return json.dumps(res)
def __init__(self, steer): # 저장된 YOLO 모델을 호출함 self.steer = steer ''' self.net = Detector(bytes("YOLOv3/cfg/Noruway.cfg", encoding="utf-8"), bytes("YOLOv3/cfg/Noruway_200.weights", encoding="utf-8"), 0, bytes("YOLOv3/cfg/Noruway.data", encoding="utf-8")) ''' self.net = Detector(bytes("YOLOv3/cfg/yolov1.cfg", encoding="utf-8"), bytes("YOLOv3/cfg/yolo.weights", encoding="utf-8"), 0, bytes("YOLOv3/cfg/coco.data", encoding="utf-8")) print("ENTERED INIT")
def get_vacant_spots_from_image(image): net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) img = Image(image) results = net.detect(img) print(results) for cat, score, bounds in results: x, y, w, h = bounds return ""
def process(record): values = json.loads(record[1]) data = values['data'] videoid = values['video_id'] metadata = values['metadata'] frameNum = metadata['id'] nparr = np.reshape( np.frombuffer(base64.b64decode(data.encode('utf8')), np.uint8), (metadata['rows'], metadata['cols'], metadata['channels'])) start_time = time.time() frame = Image(nparr) net = Detector(bytes("yolov3.cfg", encoding="utf-8"), bytes("yolov3.weights", encoding="utf-8"), 0, bytes("coco.data", encoding="utf-8")) results = net.detect(frame) del frame end_time = time.time() # print("Elapsed Time:",end_time-start_time) categories = [] for cat, score, bounds in results: x, y, w, h = bounds categories.append(str(cat.decode("utf-8"))) cv2.rectangle(nparr, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0)) cv2.putText(nparr, str(cat.decode("utf-8")), (int(x), int(y)), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255)) #cv2.imwrite('yolo-output/'+str(frameNum)+'.jpg', nparr) # print('process done') # save to hbase yoloframe = 'cts:%s' % frameNum #metadata1 = 'mtd:%s' % frameNum labels = 'lbs:%s' % frameNum # metadata = json.dumps(metadata).encode() categ = json.dumps(categories).encode() key_vals = { yoloframe.encode(): base64.b64encode(nparr), labels.encode(): categ } key_vals[b'mtd:rows'] = str(metadata['rows']).encode('utf8') key_vals[b'mtd:cols'] = str(metadata['cols']).encode('utf8') key_vals[b'mtd:channels'] = str(metadata['channels']).encode('utf8') key_vals[b'mtd:fr'] = str(metadata['frame_rate']).encode('utf8') key_vals[b'mtd:dur'] = str(metadata['duration']).encode('utf8') key_vals[b'mtd:fc'] = str(metadata['total_frames']).encode('utf8') return (videoid.encode(), key_vals)
def __init__(self, path_cfg, path_weights, path_meta, GPU="0", compute_method="compute_logits", viz_method="visualize_logits", batch_size=1, prune_coco=False, resolution=416): self.path_cfg = path_cfg self.path_weights = path_weights self.path_meta = path_meta os.environ["CUDA_VISIBLE_DEVICES"] = GPU self.compute_method = compute_method self.viz_method = viz_method self.batch_size=batch_size self.prune_coco=prune_coco # TODO: explore the lower resolution training, not right now self.resolution = resolution if prune_coco: print("Warning: pruning logits for COCO activations") # has to replace the batch size self.path_cfg_batch = self.path_cfg + ".batch_temp" + str(batch_size) cmd = "sed 's/batch=1/batch="+str(batch_size)+"/g' < " + self.path_cfg + " > " + self.path_cfg_batch print(cmd) call(cmd, shell=True) ''' # python3 syntax self.net = Detector(bytes(self.path_cfg_batch, encoding="utf-8"), bytes(self.path_weights, encoding="utf-8"), 0, bytes(self.path_meta, encoding="utf-8")) ''' self.net = Detector(self.path_cfg_batch, self.path_weights, 0, self.path_meta) self.current_images = None # some visualization functions self.cm_hot = plt.cm.get_cmap('viridis') self.norm = plt.Normalize()
def network_conf(): net = Detector( bytes("data/vehicle-detector/yolo-voc.cfg", encoding="utf-8"), bytes("data/vehicle-detector/yolo-voc.weights", encoding="utf-8"), 0, bytes("data/vehicle-detector/voc.data", encoding="utf-8")) return net
def __init__(self, config): if 'weights' in config: self.weights = bytes(config['weights'], encoding='utf-8') else: raise KeyError('weights key is missing') if 'data' in config: self.data = bytes(config['data'], encoding='utf-8') else: raise KeyError('data key is missing') if 'cfg' in config: self.cfg = bytes(config['cfg'], encoding='utf-8') else: raise KeyError('cfg key is missing') print(self.data, self.cfg, self.weights) # Init the network self.net = Detector(self.cfg, self.weights, 0, self.data)
class YoloV3: def __init__(self, config): if 'weights' in config: self.weights = bytes(config['weights'], encoding='utf-8') else: raise KeyError('weights key is missing') if 'data' in config: self.data = bytes(config['data'], encoding='utf-8') else: raise KeyError('data key is missing') if 'cfg' in config: self.cfg = bytes(config['cfg'], encoding='utf-8') else: raise KeyError('cfg key is missing') print(self.data, self.cfg, self.weights) # Init the network self.net = Detector(self.cfg, self.weights, 0, self.data) def predict(self, frame): dark_frame = Image(frame) results = self.net.detect(dark_frame) del dark_frame return results def segment(self, frame, classes=[]): predictions = self.predict(frame) for prediction in predictions: classLabel, confidence, (x, y, w, h) = prediction x1, x2, y1, y2 = int(x-w/2), int(x+w/2), int(y-h/2), int(y+h/2) segment = frame[y1: y2,x1: x2,:] yield classLabel, confidence, segment
class Object_Detection(): def __init__(self, steer): self.steer = steer ''' self.net = Detector(bytes("YOLOv3/cfg/Noruway.cfg", encoding="utf-8"), bytes("YOLOv3/cfg/Noruway_200.weights", encoding="utf-8"), 0, bytes("YOLOv3/cfg/Noruway.data", encoding="utf-8")) ''' print("ENTERED INIT") self.net = Detector( bytes("YOLOv3/cfg/yolov2.cfg", encoding="utf-8"), bytes("YOLOv3/cfg/yolov2.weights", encoding="utf-8"), 0, bytes("YOLOv3/cfg/coco.data", encoding="utf-8")) print("Exiting INIT") # with open("YOLOv3/data/coco.name", 'r') as f: # self.classes = [line.strip() for line in f.readlines()] # # self.COLORS = np.random.uniform(0, 255, size=(len(self.classes), 3)) def Detection(self, img): #print("test0") param = Image(img) #print(param) #print("test1") results = self.net.detect(param) #print("RESULTS: ", results) detect_list = [] #print("list: ", detect_list) # by hcw for cat, score, bounds in results: x, y, w, h = bounds # print('x type :', type(x)) # print('y type :', type(y)) #print(bounds) # if np.isnan(x): # print('nan') # else: color = np.random.uniform(0, 255, size=3) cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), color, thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x - w / 2), int(y - h / 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color) detect_list.append(cat.decode()) #print(detect_list) #print("test") cv2.imshow('dect', img) #print("test2") cv2.waitKey(1) # hcw self.steer.Set_ObjectDetection(detect_list)
class Object_Detection(): def __init__(self, steer): # 저장된 YOLO 모델을 호출함 self.steer = steer self.net = Detector( bytes("YOLOv3/cfg/yolo-obj.cfg", encoding="utf-8"), bytes("YOLOv3/cfg/yolo-obj_400.weights", encoding="utf-8"), 0, bytes("YOLOv3/cfg/obj.data", encoding="utf-8")) # 이미지를 입력받아 detection과 classification 결과를 리턴함 def Detection(self, img): results = self.net.detect(Image(img)) detect_list = [] for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x - w / 2), int(y + h / 4)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255)) detect_list.append(cat.decode()) cv2.imshow('dect', img) self.steer.Set_ObjectDetection(detect_list)
def __init__(self, image_data, yolo_dir, score_thresh=0.5, fps=0.08): LOGGER.info("Directory: " + yolo_dir) self.createDataFile(yolo_dir) data = glob.glob(yolo_dir + '*.data')[0] config = glob.glob(yolo_dir + '*.cfg')[0] weights = glob.glob(yolo_dir + '*.weights')[0] cls_names = glob.glob(yolo_dir + '*.names')[0] LOGGER.info("Yolo data: %s" % data) LOGGER.info("Yolo config: %s" % config) LOGGER.info("Yolo weights: %s" % weights) LOGGER.info("Yolo class names: %s" % cls_names) self.createClassNames(yolo_dir, cls_names) self.done = False threading.Thread.__init__(self) self.pause = False self.name = "YOLO Predictor Thread" self.image_data = image_data LOGGER.info("Launching detector ...") self.net = net = Detector(bytes(config, encoding="utf-8"), bytes(weights, encoding="utf-8"), 0, bytes(data, encoding="utf-8")) self.results = [] self.output_data = OutputClassificationData() self.output_data.score_thresh = score_thresh self.frames_per_ms = fps LOGGER.info("Score thresh: %s" % score_thresh) LOGGER.info("FPS: %s" % fps)
class YOLO: # Optional statement to configure preferred GPU. Available only in GPU version. # pydarknet.set_cuda_device(0) def __init__(self): self.net = Detector( bytes("cfg/danyolo.cfg", encoding="utf-8"), bytes("weights/danyolo_900.weights", encoding="utf-8"), 0, bytes("cfg/obj.names", encoding="utf-8")) # Only measure the time taken by YOLO and API Call overhead def detect(self, frame): dark_frame = Image(frame) results = self.net.detect(dark_frame) del dark_frame bboxes = [] cats = [] scores = [] for cat, score, bounds in results: x, y, w, h = bounds bboxes.append((int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2))) cats.append(cat) scores.append(score) return bboxes, cats, scores
class Object_Detection(object): def __init__(self, steer): self.steer = steer # Detection 및 Classification에 사용할 모델 구조(cfg), 학습한 가중치 결과(weights), label 분류 파일(data) 파일을 # Detector의 매개변수로 지정한다. self.net = Detector(bytes("./yolo/cfg/yolo-obj.cfg", encoding="utf-8"), bytes("./yolo/cfg/yolo-obj_400.weights", encoding="utf-8"), 0, bytes("./yolo/obj.data", encoding="utf-8")) def detection(self, img): results = self.net.detect(Image(img)) detect_list = [] for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img, str(cat.decode("utf-8")), (int(x - w / 2), int(y + h / 4)), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255)) detect_list.append(cat.decode()) cv2.imshow('dect', img) self.steer.Set_ObjectDetection(detect_list)
class DarknetYoloV3Analyze: def __init__(self): self.net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("wgt/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data", encoding="utf-8")) def analyze(self, filename: str, folder: str) -> AnnotationModel: path = folder+'/'+filename frame = cv2.imread(path) dark_frame = Image(frame) results = self.net.detect(dark_frame) ah, aw, ad = frame.shape annotation_size = AnnotationSize(aw, ah, ad) objects = [] for cat, score, bounds in results: x, y, w, h = bounds # print("output: ", x, " ", y, " ", w, " ", h, "tot: ", aw, " ", ah) bndbox = { 'xmin': x-w/2, 'xmax': x+w/2, 'ymin': y-h/2, 'ymax': y+h/2, } if cat.decode('utf-8') == 'person': objects += [AnnotationObjectModel(str('0'), bndbox)] annotation_model = AnnotationModel(filename, folder, folder+'/'+filename, annotation_size, objects) return annotation_model
class ObjectDetection: def __init__(self): root = '/app' configPath = root + '/brain/yolov3.cfg' weightsPath = root + '/brain/yolov3.weights' classesPath = root + '/brain/coco.data' self.net = Detector(bytes(configPath, encoding="utf-8"), bytes(weightsPath, encoding="utf-8"), 0, bytes(classesPath, encoding="utf-8")) def run(self, frame): dark_frame = Image(frame) results = self.net.detect(dark_frame) # print(serializedResult) # cv2.imshow('frame',frame) # if cv2.waitKey(1) & 0xFF == ord('q'): # break #del dark_frame return results
def load_detector(): # constructor, load detector code_dir = "/home/tom/Projects/SLAM/vehicle_detection/" net = Detector(bytes(code_dir + "cfg/yolov3.cfg", encoding="utf-8"), bytes(code_dir + "cfg/yolov3.weights", encoding="utf-8"), 0, bytes(code_dir + "cfg/coco.data", encoding="utf-8")) return net
# clone + dw models + create file + run test! git clone https://pypi.org/project/yolo34py/ cd yolo34py sh download_models.sh wget -C http://absfreepic.com/absolutely_free_photos/small_photos/crowded-cars-on-street-4032x2272_48736.jpg # create python file with folowing code and run """ from pydarknet import Detector, Image import cv2 cv2.__version__ net = Detector(bytes("cfg/yolov3.cfg", encoding="utf-8"), bytes("weights/yolov3.weights", encoding="utf-8"), 0, bytes("cfg/coco.data" ,encoding="utf-8")) img = cv2.imread('crowded-cars-on-street-4032x2272_48736.jpg') img_darknet = Image(img) results = net.detect(img_darknet) for cat, score, bounds in results: x, y, w, h = bounds cv2.rectangle(img, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (255, 0, 0), thickness=2) cv2.putText(img ,str(cat.decode("utf-8")) ,(int(x) ,int(y)) ,cv2.FONT_HERSHEY_COMPLEX ,1 ,(255 ,255 ,0)) cv2.imshow("output", img) cv2.waitKey(0) """