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, yolo_dir, mode=0): #darknet.set_gpu(gpu_num) self.metaMain = None self.netMain = None altNames = None configPath = None weightPath = None # Use tiny yolov3 if(mode == 0): configPath = os.path.join(yolo_dir, "cfg/tiny-yolo.cfg") weightPath = os.path.join(yolo_dir, "yolov3-tiny.weights") # Use yolov3 elif(mode == 1): configPath = os.path.join(yolo_dir, "cfg/yolov3.cfg") weightPath = os.path.join(yolo_dir, "yolov3.weights") metaPath = os.path.join(yolo_dir, "cfg/coco.data") if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath)+"`") if self.netMain is None: self.netMain = darknet.load_net_custom(configPath.encode( "ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if self.metaMain is None: self.metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError as e: print(e) pass except Exception as e: print(e) pass self.darknet_image = darknet.make_image(darknet.network_width(self.netMain), darknet.network_height(self.netMain),3)
def _initialize_darknet(self): # Setup paths inputDir = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "input") configPath = os.path.join(inputDir, "yolov3.cfg") weightPath = os.path.join( inputDir, "card.weights") # Don't change this - change the file name instead metaPath = os.path.join(inputDir, "obj.data") setupObjPaths(metaPath, os.path.join(inputDir, "obj.names")) if not os.path.exists(weightPath): print(f"Couldn't find weights path '{weightPath}'") sys.exit(101) self._darknet_netMain = darknet.load_net_custom( configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 self._darknet_metaMain = darknet.load_meta(metaPath.encode("ascii")) print("Finished loading") # Not sure what is going on here? try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError as e: print(e) sys.exit(1337) except Exception as e: print(e) sys.exit(1337) self._darknet_resolution = (darknet.network_width( self._darknet_netMain), darknet.network_height( self._darknet_netMain)) self._darknet_image = darknet.make_image(self._darknet_resolution[0], self._darknet_resolution[1], 3)
def imageDetection(image, network, class_names, class_colors, thresh): # Modified from darknet_images.py width = darknet.network_width(network) height = darknet.network_height(network) darknet_image = darknet.make_image(width, height, 3) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh) darknet.free_image(darknet_image) image = darknet.draw_boxes(detections, image_resized, class_colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
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), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh) image = darknet.draw_boxes(detections, image_resized, class_colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
def setup(self, netMain=None, metaMain=None, altNames=None): # Load the model if netMain is None: self.netMain = darknet.load_net_custom( self.configPath.encode('ascii'), self.weightPath.encode('ascii'), 0, 1) # batch size = 1 else: self.netMain = netMain if metaMain is None: self.metaMain = darknet.load_meta(self.metaPath.encode('ascii')) else: self.metaMain = metaMain if altNames is None: try: with open(self.metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search('names *= *(.*)$', metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if osp.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split('\n') self.altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass else: self.altNames = altNames # Create an image we reuse for each detect self.darknet_image = darknet.make_image( darknet.network_width(self.netMain), darknet.network_height(self.netMain), 3) # Performance logging self.count = 0 self.total = 0
def model(self, img: Image) -> (np.array, set): W, H = img.size img = np.asarray(img) result_set = set() darknet_image = dn.make_image(int(W), int(H), 3) frame_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) dn.copy_image_from_bytes(darknet_image, frame_rgb.tobytes()) # im = nparray_to_image(arr) R = detect_image(self.vehicle_net, self.vehicle_meta, darknet_image, thresh=self.vehicle_threshold) R = [r for r in R if r[0].decode('utf-8') in ['car', 'bus', 'truck']] if len(R): WH = np.array(img.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Lcars.append(label) Icar = crop_region(img, label) # print('Searching for license plates using WPOD-NET') ratio = float(max(Icar.shape[:2])) / min(Icar.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) # print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(self.license_model, Icar / 255, bound_dim, 2**4, (240, 80), 0.5) if len(LlpImgs): Ilp = LlpImgs[0] res, confidence = self.ocr_model.recognizeOneframe(Ilp * 255.) pts = Llp[0].pts * label.wh().reshape( 2, 1) + label.tl().reshape(2, 1) ptspx = pts * np.array(img.shape[1::-1], dtype=float).reshape(2, 1) draw_losangle(img, ptspx, RED, 3) if confidence > 0.5: llp = Label(0, tl=pts.min(1), br=pts.max(1)) img = write2img(img, llp, res) result_set.add(res) for i, lcar in enumerate(Lcars): draw_label(img, lcar, color=YELLOW, thickness=3) # 如果没检测到车,就直接检测-识别车牌(需要改进) # ToDO else: # print('Searching for license plates using WPOD-NET') ratio = float(max(W, H)) / min(W, H) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) # print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(self.license_model, img / 255.0, bound_dim, 2**4, (240, 80), 0.5) if len(LlpImgs): Ilp = LlpImgs[0] res, confidence = self.ocr_model.recognizeOneframe(Ilp * 255.) ptspx = Llp[0].pts * np.array(img.shape[1::-1], dtype=float).reshape(2, 1) draw_losangle(img, ptspx, RED, 3) if confidence > 0.5: llp = Label(0, tl=Llp[0].pts.min(1), br=Llp[0].pts.max(1)) img = write2img(img, llp, res) result_set.add(res) # 没检测到车牌,就直接尝试识别车牌 else: res, confidence = self.ocr_model.recognizeOneframe(img) if confidence > 0.5: result_set.add(res) return img, result_set
def first_phase(self, start_id): # read the images batch and run detections end_id = min(self.total_frame, int(start_id) + self.batch_size) print(end_id) total_obj = 0 unique_obj_bbox = {} displacement_check = self.displacement_check for i in range(int(start_id), end_id): # print(self.dataset_dir + "/" + f"{str(i).zfill(10)}.png") image = cv2.imread(self.dataset_dir + "/" + f"{str(i).zfill(10)}.png") darknet_image = darknet.make_image(1920, 1080, 3) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) darknet.copy_image_from_bytes(darknet_image, image_rgb.tobytes()) # detections list of tuple: (class_name, confidence_score, (bbox_info)) detections = darknet.detect_image(self.network, self.class_names, darknet_image, thresh=0.4) total_obj = total_obj + len(detections) bboxes = [obj[2] for obj in detections] confidence = [obj[1] for obj in detections] classes = [obj[0] for obj in detections] features = self.encoder(image_rgb, bboxes) detections = [Detection(bbox, confidence, cls, feature) for bbox, confidence, cls, feature in zip(bboxes, confidence, classes, features)] # Run non-maxima suppression. boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) indices = preprocessing.non_max_suppression(boxes, self.nms_max_overlap, scores) detections = [detections[i] for i in indices] self.tracker.predict() self.tracker.update(detections) for track in self.tracker.tracks: if not track.is_confirmed() or track.time_since_update > 1: continue bbox = track.to_tlbr() if track.track_id not in unique_obj_bbox: # unique_obj_bbox.append(track.track_id) unique_obj_bbox[track.track_id] = {'feature': track.features[0].tolist()} unique_obj_bbox[track.track_id]['length'] = 0 # find the center point of the tracking object center_point = track.to_tlwh() c_x = center_point[0] + (center_point[2]) / 2 c_y = center_point[1] + (center_point[3]) / 2 if track.track_id not in displacement_check: displacement_check[track.track_id] = (c_x, c_y) else: disp = math.sqrt((c_x - displacement_check[track.track_id][0]) ** 2 + (c_y - displacement_check[track.track_id][1]) ** 2) # print(unique_obj_bbox[track.track_id]) # print('disp for cam: ', str(track.track_id), " ", str(disp)) unique_obj_bbox[track.track_id]['length'] = unique_obj_bbox[track.track_id]['length'] + disp # update the center point for next iteration displacement_check[track.track_id] = (c_x, c_y) # print(displacement_check) # print(unique_obj_bbox[track.track_id]['length']) self.displacement_check = displacement_check # print(displacement_check) return {'total_obj': total_obj, 'unique_obj_bbox': unique_obj_bbox}
def __enter__(self): self._img = darknet.make_image(self.w, self.h, 3) return self
def YOLO(): global metaMain, netMain, altNames configPath = "requirements/yolov3_custom.cfg" weightPath = "weights/yolov3_custom_last.weights" metaPath = "requirements/detector.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) videopath = argv[1] if os.path.exists(videopath): name = videopath.split('/')[-1].split('.')[0] cap = cv2.VideoCapture(videopath) else: print("Incorrect path to video") return cap.set(3, 1280) cap.set(4, 720) out = cv2.VideoWriter( "detections/" + name + "_output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize( frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.1) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(1 / (time.time() - prev_time)) cap.release() out.release()
def YOLO(): global metaMain, netMain, altNames configPath = "Configuration/yolov4_obj.cfg" weightPath = "Weights/yolov4_obj_final.weights" metaPath = "Configuration/detector.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass #cap = cv2.VideoCapture(0) imagepath = argv[1] if os.path.exists(imagepath): name = imagepath.split('/')[-1].split('.')[0] frame_read = cv2.imread(imagepath) else: print("Incorrect path to image") return print("Starting the YOLO loop...") inputshape = (darknet.network_width(netMain), darknet.network_height(netMain)) darknet_image = darknet.make_image(inputshape[0], inputshape[1], 3) prev_time = time.time() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, inputshape, interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.2) image = cvDrawBoxes(detections, frame_rgb, inputshape) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print("FPS : ", end='') print(1.0 / (time.time() - prev_time)) cv2.imwrite(name + "_output.jpg", image)
if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) movable = [ 'bicycle', 'bus', 'car', 'carrier', 'cat', 'dog', 'motorcycle', 'movable_signage', 'person', 'scooter', 'stroller', 'truck', 'wheelchair' ] fixed = [ 'barricade', 'bench', 'bollard', 'chair', 'fire_hydrant', 'kiosk', 'parking_meter', 'pole', 'potted_plant', 'power_controller', 'stop', 'table', 'traffic_light', 'traffic_light_controller', 'traffic_sign', 'tree_trunk' ] def convertBack(x, y, w, h): xmin = int(round(x - (w / 2)))
def main(): global metaMain, netMain, altNames configPath = "darknet/cfg/yolov3-seabird.cfg" weightPath = "darknet/backup_608/yolov3-seabird_final.weights" metaPath = "darknet/cfg/seabird.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath)+"`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode( "ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass # Definition of the parameters max_cosine_distance = 0.3 max_euclidean_distance = 150.0 nn_budget = None nms_max_overlap = 1 # deep_sort metric = nn_matching.NearestNeighborDistanceMetric("euclidean", max_euclidean_distance, nn_budget) #metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) writeVideo_flag = False #video_capture = cv2.VideoCapture('/data/Farallon3_20190706_000001_No001.avi') #video_capture = cv2.VideoCapture('/data/15_fps/Farallon3_20190620_021546_No001.mp4') video_capture = cv2.VideoCapture('/data/15_fps/Farallon3_20190621_090300_No004.mp4') #video_capture = cv2.VideoCapture('/data/rows_data/15_fps/Farallon3_20190603_155007_No001.avi') video_fps = video_capture.get(cv2.CAP_PROP_FPS) #video_fps = 25 list_file = open('events.csv', 'w') track_log_file = open('track_log.csv', 'w') wr = csv.writer(list_file, dialect='excel') wr_tracklog = csv.writer(track_log_file, dialect='excel') if writeVideo_flag: # Define the codec and create VideoWriter object w = int(video_capture.get(3)) h = int(video_capture.get(4)) fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('1.avi', fourcc, 15, (w, h)) zone = cv2.imread('mask/test_zone.png', -1) # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain),3) fps = 0.0 frame_index = -1 mask = cv2.imread('mask/mask_new.jpg') mask = np.uint8(mask/255) ############################################## #points = [(130,102),(535,370),(808,345),(1570,391),(1494,808),(373,817),(4,496),(1,276)] #points = [(4,22),(121,96),(207,99),(537,366),(819,324),(1564,385),(1764,322),(1648,889),(105,915),(3,762)] points = [(2,24),(127,24),(579,321),(1746,336),(1674,878),(1912,957),(1926,1081),(2,1074)] zone_polygon = Polygon(points) text_count_size = 9e-3 * 200 text_count_x,text_count_y = 550, 1000 avg_area_box_all_frames = 0 time_stamp = datetime(2019, 6, 21, 10, 33, 5) while True: ret, frame = video_capture.read() # frame shape 640*480*3 if ret != True: break frame_index = frame_index +1 t1 = time.time() write = 0 if frame_index % 15 == 0: write = 1 draw_frame = frame frame = np.multiply(frame, mask) # image = Image.fromarray(frame) #image = Image.fromarray(frame[...,::-1]) #bgr to rgb frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.5) boxs, class_ids=convert(detections, dw, dh) # score to 1.0 here). dets = [Detection(bbox, 1.0, class_id) for bbox,class_id in zip(boxs, class_ids)] # Run non-maxima suppression. boxes = np.array([d.tlwh for d in dets]) scores = np.array([d.confidence for d in dets]) indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores) detections_tracker = [dets[i] for i in indices] ids = [class_ids[i] for i in indices] time_lapsed_sec = frame_index / video_fps time_stamp_now = time_stamp + timedelta(seconds=time_lapsed_sec) # Call the tracker tracker.predict() tracker.update(detections_tracker, ids, time_stamp_now) tracker.update_events(draw_frame, time_stamp_now, wr, wr_tracklog, write ) avg_area_box = 0 for det in detections: name , x, y, w, h = det[0],det[2][0],det[2][1],det[2][2],det[2][3] class_id = altNames.index(name.decode("utf-8") ) bbox = [(x-w/2)*dw,(y-h/2)*dh,(x+w/2)*dw,(y+h/2)*dh] area_box = w*h*dw*dh if area_box > 3*avg_area_box_all_frames: cv2.rectangle(draw_frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,255,255), 3) cv2.putText(draw_frame,"Flapping detected !!!!",(text_count_x,text_count_y),4,text_count_size, (255,0,0),4) avg_area_box = avg_area_box + area_box cv2.rectangle(draw_frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),get_rgb(class_id,3), 2) avg_area_box = avg_area_box/len(detections) avg_area_box_all_frames=avg_area_box if writeVideo_flag: # save a frame out.write(draw_frame) fps = ( fps + (1./(time.time()-t1)) ) / 2 print("fps= %f"%(fps)) video_capture.release() if writeVideo_flag: out.release() list_file.close() cv2.destroyAllWindows()
def YOLO(): #os.chdir("./darknet") #os.system("pwd") #print('dir changed!') from darknet import darknet print("Imported") #================================================================ base_dir = get_base_dir() image_list = read_image() #=================================================================# # Read parking regions file parked_cars = read_regions() #================================================================ global metaMain, netMain, altNames configPath = "./darknet/cfg/yolov4.cfg" weightPath = "./darknet/yolov4.weights" metaPath = "./darknet/cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass # i = 0 # while True: for i in range(len(image_list)): image = cv2.imread(image_list[i]) width = image.shape[1] height = image.shape[0] # Create an image we reuse for each detect darknet_image = darknet.make_image(width, height, 3) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_rgb = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, image_rgb.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) # Print detections # image = cvDrawBoxesonCars(detections, image_rgb) # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Draw occupied parking spaces image, parked_cars_updated = cvOverlapcheck(parked_cars, detections, image_rgb) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) from cv2 import imwrite file2 = os.path.join(base_dir + '/Api/templates/static/images/new_output.jpg') imwrite(file2, image) print("Image saved!") # cv2.imshow('Output', image) cv2.waitKey(0) # i += 1 cv2.destroyAllWindows() #================================================================ # Save the file with occupancy post_process(parked_cars_updated)
def YOLO(): global metaMain, netMain, altNames configPath = "Configuration/yolov4_obj.cfg" weightPath = "Weights/yolov4_obj_final.weights" metaPath = "Configuration/detector.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath) + "`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath) + "`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath) + "`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass videopath = argv[1] if os.path.exists(videopath): name = videopath.split('/')[-1].split('.')[0] cap = cv2.VideoCapture(videopath) else: print("Incorrect path to video") return cap = cv2.VideoCapture(videopath) fps = cap.get(cv2.CAP_PROP_FPS) print(fps) cap.set(3, 1280) cap.set(4, 720) ret, frame_read = cap.read() out = cv2.VideoWriter(name + "_output.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (frame_read.shape[1], frame_read.shape[0])) print("Starting the YOLO loop...") inputshape = (darknet.network_width(netMain), darknet.network_height(netMain)) # Create an image we reuse for each detect darknet_image = darknet.make_image(inputshape[0], inputshape[1], 3) start = time.time() cnt = 1 while ret: cnt += 1 prev_time = time.time() if ret: frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, inputshape, interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) image = cvDrawBoxes(detections, frame_rgb, inputshape) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(1 / (time.time() - prev_time)) out.write(image) ret, frame_read = cap.read() # cv2.imshow('Demo', image) #uncomment if running on local machine # cv2.waitKey(3) #uncomment if running on local machine print() print("Average FPS : ", end='') print(cnt * 1.0 / (time.time() - start)) cap.release() out.release()