# load the Neural Network and the meta path = "./char/" cfg = os.path.join(path, "yolov3_plate.cfg").encode() weights = os.path.join(path, "yolov3_plate_4000_ok.weights").encode() data = os.path.join(path, "obj_plate.data").encode() net = dn.load_net(cfg, weights, 0) meta = dn.load_meta(data) path_v = "/home/jehl/darknet/char/img_char" files = os.listdir(path_v) for f in files: img_path = os.path.join(path_v, f) result, im = dn.detect(net, meta, img_path.encode(), thresh=0.1) if len(result) != 0: print("Recognize !!!!!!!!!!!!!!!!!!") for p in result: x1 = int(p[2][0] - p[2][2] / 2) y1 = int(p[2][1] - p[2][3] / 2) x2 = int(p[2][0] + p[2][2] / 2) y2 = int(p[2][1] + p[2][3] / 2) #cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),3) #cv2.putText(frame, p["plate"], (x1,y1-5), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,255,0),2) h, w = im.h, im.w center = (p[2][0], p[2][1]) hb, wb = (p[2][3], p[2][2]) #cv2.circle(frame, center, 10, (255,0,0),2) #cv2.line(frame, (center[0]-wb/2,center[1]), (center[0]+wb/2,center[1]), (255,0,0),3) #cv2.line(frame, (center[0],center[1]-hb/2), (center[0],center[1]+hb/2), (255,0,0),3)
def detection(image, net, meta): res = dn.detect(net, meta, str.encode(image)) return res
input_dir = "../video/" output_path = "../output.mp4" data = "cfg/aerial.data" cfg = "cfg/yolov3-aerial.cfg" weights = "backup/yolov3-aerial.backup" images = [d for d in os.listdir(input_dir) if d.endswith('.jpg')] images.sort() img = cv2.imread(images[0]) height, width, channels = img.shape net = dn.load_net(cfg.encode(), weights.encode(), 0) meta = dn.load_meta(data.encode()) fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output_path, fourcc, 20.0, (width, height)) count = 0 total = len(images) for i in images: count += 1 print("Exportation {}%".format(count / total), end="\r") img_path = os.path.join(input_dir, i) r = dn.detect(net, meta, im.encode()) out.write(draw_yolo_result(im, r, ['car', 'bus', 'truck'])) out.release() print("Exported {}".format(output_path))
import python.darknet as dn dn.set_gpu(0) net = dn.load_net(str.encode("cfg/tiny-yolo.cfg"), str.encode("weights/tiny-yolo.weights"), 0) meta = dn.load_meta(str.encode("cfg/coco.data")) r = dn.detect(net, meta, str.encode("data/dog.jpg")) print(r) # And then down here you could detect a lot more images like: # r = dn.detect(net, meta, "data/eagle.jpg") # print(r) # r = dn.detect(net, meta, "data/giraffe.jpg") # print(r) # r = dn.detect(net, meta, "data/horses.jpg") # print(r) # r = dn.detect(net, meta, "data/person.jpg") # print(r)
cv2.putText(img, detection[0].decode() + " [" + str(round(detection[1] * 100, 2)) + "]", (pt1[0], pt1[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0], 2) return img # In[ ]: n = 0 while True: try: # frame = cap.get_frame() _,frame = cap.read() # W,H = frame.shape[:2] # print(W,H) detected = detect(net,meta,frame,thresh=0.5) frame = cvDrawBoxes(detected, frame) cv2.waitKey(1) cv2.imshow('frame',frame) except Exception as e: print(e) # In[ ]: