def init_models(basic_path): print("\n [*]Loading object detection model!\n") det_net = darknet.load_net( str.encode(basic_path) + b"darknet/cfg/yolov3-food.cfg", str.encode(basic_path) + b"darknet/backup/food/yolov3-food_final.weights", 0) det_meta = darknet.load_meta( str.encode(basic_path) + b"darknet/cfg/food.data") print("\n [*]Loading object classficiation model!\n") classes = 231 base_model = Xception(include_top=True, input_shape=(299, 299, 3)) base_model.layers.pop() predictions = Dense(classes, activation='softmax')(base_model.layers[-1].output) clf_model = Model(input=base_model.input, output=[predictions]) clf_model.load_weights(basic_path + "classification/models/xception-0-15-0.82.h5") class_dict = { v: k for k, v in np.load(basic_path + "classification/class_index/food231.npy")[( )].items() } return det_net, det_meta, clf_model, class_dict
def process(): file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) adaptive_resize(os.path.join(app.config['UPLOAD_FOLDER'], filename)) sys.path.append(os.path.join(os.path.dirname(__file__), 'darknet')) import darknet.darknet as dn #net = dn.load_net(bytes("darknet/cfg/tiny-yolo.cfg", 'ascii'), bytes("darknet/tiny-yolo.weights", 'ascii'), 0) net = dn.load_net(bytes("darknet/cfg/yolo.cfg", 'ascii'), bytes("darknet/yolo.weights", 'ascii'), 0) meta = dn.load_meta(bytes("darknet/cfg/coco.data", 'ascii')) r = dn.detect(net, meta, bytes(app.config['UPLOAD_FOLDER'] + filename, 'ascii'), thresh=.5) res = '' for object in r: name = object[0].decode('utf-8') prob = float(object[1]) rct = object[2] res += 'Detected <b>' + name + '</b> with probability of ' + str( round(prob, 2)) #res += ' ' + str(rct) res += '<br />' draw_object(app.config['UPLOAD_FOLDER'] + filename, rct) return render_template('result.html', filename='uploads/' + filename + '?' + str(randint(0, 999)), result_text=res) else: return 'invalid data'
def model_init(): net = darknet.load_net( "/root/htsc_detect/darknet/htsc_test.cfg".encode('utf-8'), "/root/htsc_detect/darknet/htsc.weights".encode('utf-8'), 0) #模型载入 meta = darknet.load_meta( "/root/htsc_detect/darknet/htsc.data".encode('utf-8')) #Processor载入 return net, meta
def load_system(): try: loaded_models = [] vehicle_threshold = .5 vehicle_weights = 'data/vehicle-detector/vehicle-detection.weights' vehicle_netcfg = 'data/vehicle-detector/vehicle-detection.cfg' vehicle_dataset = 'data/vehicle-detector/vehicle-detection.data' lp_threshold = .25 lp_weights = 'data/lp-detector/lpd.weights' lp_netcfg = 'data/lp-detector/lpd.cfg' lp_dataset = 'data/lp-detector/lpd.data' ocr_threshold = .4 ocr_weights = 'data/cr/cr.weights' ocr_netcfg = 'data/cr/cr.cfg' ocr_dataset = 'data/cr/cr.data' vehicle_net = dn.load_net(vehicle_netcfg.encode('utf-8'), vehicle_weights.encode('utf-8'), 0) vehicle_meta = dn.load_meta(vehicle_dataset.encode('utf-8')) loaded_models.append([vehicle_net, vehicle_meta, vehicle_threshold]) lp_net = dn.load_net(lp_netcfg.encode('utf-8'), lp_weights.encode('utf-8'), 0) lp_meta = dn.load_meta(lp_dataset.encode('utf-8')) loaded_models.append([lp_net, lp_meta, lp_threshold]) ocr_net = dn.load_net(ocr_netcfg.encode('utf-8'), ocr_weights.encode('utf-8'), 0) ocr_meta = dn.load_meta(ocr_dataset.encode('utf-8')) loaded_models.append([ocr_net, ocr_meta, lp_threshold]) return loaded_models except: traceback.print_exc() sys.exit(1) sys.exit(0)
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 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 patch_predictor(gpu, i_queue, o_queue): os.environ["CUDA_VISIBLE_DEVICES"] = gpu # initialize yolov3 model thresh = 0.1 hier_thresh = 0.5 nms = 0.45 # config_file = "/home/ssd_array0/Develop/liyu/darknet/cfg/gnet2.net".encode('utf-8') # weights_file = "/home/ssd_array0/Develop/liyu/darknet/backup/gnet2/gnet2_200000.weights".encode('utf-8') # datacfg_file = "/home/ssd_array0/Develop/liyu/darknet/cfg/gnet2.data".encode('utf-8') try: net = load_net(config_file, weights_file, 0) meta = load_meta(datacfg_file) except: print("[ERROR] failed to load yolov3 model") return while True: item = i_queue.get() patch = item[1] predictions = detect_numpy(net, meta, patch, thresh, hier_thresh, nms) predictions_ = [] for pred in predictions: label = pred[0] probability = pred[1] cx, cy, w, h = pred[2] x, y = int(cx - w / 2), int(cy - h / 2) w, h = int(w), int(h) w, h = min(w, w + x), min(h, h + y) x, y = max(0, x), max(0, y) predictions_.append([label, probability, (x, y, w, h)]) o_queue.put( (item[0], predictions_, item[2])) # (txt_name, predictions, N) del item
import sys, os sys.path.append('../../darknet') import darknet.darknet as dn from server import app, is_authenticated from flask import request, render_template # load YOLOv4 model MODEL_CFG = 'darknet/custom5-512.cfg' MODEL_WEIGHTS = 'darknet/custom5-512.weights' MODEL_DATA = 'darknet/custom5-512.data' net = dn.load_net_custom(str.encode(MODEL_CFG), str.encode(MODEL_WEIGHTS), 0, 1) meta = dn.load_meta(str.encode(MODEL_DATA)) # start Flask Server from flask import Flask, jsonify, request, render_template from werkzeug.utils import secure_filename from flask_cors import CORS import uuid from PIL import Image ALLOWED_EXTENSIONS = {'png', 'jpg'} #utils def allowed_file(filename): return '.' in filename and filename.rsplit( '.', 1)[1].lower() in ALLOWED_EXTENSIONS
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()
# 파이썬에서 dll 로딩하여 제공하는 함수 호출 가능하게 하는 모듈 from ctypes import * #1. 사용할 변수 선언 config_path = "C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/train/my_yolov3.cfg" weigh_path = "C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/train/my_yolov3_final.weights" meta_path = "C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/train/my_data.data" video_path = "C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/data/video/16-3_cam01_assault01_place02_night_spring.mp4" threshold = 0.25 #2. 관련 파일 load # cfg(모델관련)와 weight 파일 아스키코드로 load net = darknet.load_net(bytes(config_path, "ascii"), bytes(weigh_path, "ascii"), 0) # data(classes, train, valid, names, backup의 경로가 명시된 텍스트파일) 아스키코드로 load meta = darknet.load_meta(bytes(meta_path, "ascii")) # 비디오의 프레임을 추출하는 VideoCapture 함수 cap = cv2.VideoCapture(video_path) print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #3. opencv로 darknet 실행 # 비디오 캡쳐 객체가 정상으로 open 되는 동안 반복 while (cap.isOpened()): # 비디오의 한 프레임씩 읽기 # 제대로 읽으면 ret=True, 읽은 프레임은 image 변수 ret, image = cap.read() # resize하기 위해서는 픽셀 사이의 값을 결정. INTER_AREA=사이즈를 줄이기 위한 보간법 image = cv2.resize(image, dsize=(640, 480), interpolation=cv2.INTER_AREA) print(image.shape)
if findList(indices, i.index) == -1: i.state = 2 i.time = time.time() if __name__ == '__main__': tracker = Sort() colors = [(random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)) for i in range(10)] dn.set_gpu(0) net = dn.load_net( "/home/bardoe/sources/sceneLight/model/yolov3.cfg".encode("utf-8"), "/home/bardoe/sources/sceneLight/model/yolov3.weights".encode("utf-8"), 0) meta = dn.load_meta( "/home/bardoe/sources/sceneLight/model/coco.data".encode("utf-8")) cap = cv2.VideoCapture(0) frames = 0 start = time.time() sender.start() sender.activate_output(1) sender[1].multicast = True while cap.isOpened(): if frames > 100: frames = 0 start = time.time() _, frame = cap.read() dmx_data = list([0] * 13)
if __name__ == '__main__': try: input_dir = sys.argv[1] output_dir = sys.argv[2] vehicle_threshold = .5 vehicle_weights = 'data/vehicle-detector/vehicle-detection.weights' vehicle_netcfg = 'data/vehicle-detector/vehicle-detection.cfg' vehicle_dataset = 'data/vehicle-detector/vehicle-detection.data' vehicle_net = dn.load_net(vehicle_netcfg.encode('utf-8'), vehicle_weights.encode('utf-8'), 0) vehicle_meta = dn.load_meta(vehicle_dataset.encode('utf-8')) imgs_paths = image_files_from_folder(input_dir) imgs_paths.sort() if not isdir(output_dir): makedirs(output_dir) print('Searching for vehicles...') for i, img_path in enumerate(imgs_paths): print('\tScanning %s' % img_path) bname = basename(splitext(img_path)[0])
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()
from flask import Flask, render_template, Response, request # emulated camera import cv2, numpy as np from threading import Thread from darknet import darknet net = darknet.load_net(b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/cfg/yolov3.cfg", b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/weight/yolov3.weights", 0) meta = darknet.load_meta(b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/data/coco.data") cap = cv2.VideoCapture("C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/26-4_cam01_assault01_place01_night_spring.mp4") class WebcamVideoStream: def __init__(self, src=0): print("init") self.stream = cv2.VideoCapture(src) (self.grabbed, self.frame) = self.stream.read() self.stopped = False def start(self): print("start thread") t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): print("read") while True: if self.stopped: return
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()
from darknet import darknet import cv2, numpy as np, sys from ctypes import * net = darknet.load_net( b"D:/python_module/darknet-master/build/darknet/x64/project/myyolov3.cfg", b"D:/python_module/darknet-master/build/darknet/x64/project/backup/myyolov3_final.weights", 0) meta = darknet.load_meta( b"D:/python_module/darknet-master/build/darknet/x64/project/my.data") cap = cv2.VideoCapture( "D:/python_module/darknet-master/build/darknet/x64/project/22-2.mp4") fourcc = cv2.VideoWriter_fourcc(*'DIVX') fps = cap.get(cv2.CAP_PROP_FPS) out = cv2.VideoWriter('otter_out26-4_test.avi', fourcc, fps, (640, 480)) print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) i = 0 while (cap.isOpened()): i += 1 ret, image = cap.read() image = cv2.resize(image, dsize=(640, 480), interpolation=cv2.INTER_AREA) print(i) if not ret: break frame = darknet.nparray_to_image(image) r = darknet.detect_image(net, meta, frame, thresh=.5,
from darknet import darknet import cv2, numpy as np, sys from ctypes import * net = darknet.load_net( b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/cfg/myyolov3.cfg", b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/weight/myyolov3_final.weights", 0) meta = darknet.load_meta( b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/weight/my.data") cap = cv2.VideoCapture( 0 ) #"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/26-4_cam01_assault01_place01_night_spring.mp4") print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) i = 0 while (cap.isOpened()): i += 1 ret, image = cap.read() image = cv2.resize(image, dsize=(640, 480), interpolation=cv2.INTER_AREA) print(i) if not ret: break frame = darknet.nparray_to_image(image) r = darknet.detect_image(net, meta, frame, thresh=.5, hier_thresh=.5, nms=.45, debug=False)
metaPath = "./darknet/cfg/obj.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("utf-8"), weightPath.encode("utf-8"), 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")
if __name__ == '__main__': try: input_dir = sys.argv[1] output_dir = input_dir ocr_threshold = .4 ocr_weights = 'data/cr/cr.weights' ocr_netcfg = 'data/cr/cr.cfg' ocr_dataset = 'data/cr/cr.data' ocr_net = dn.load_net(ocr_netcfg.encode('utf-8'), ocr_weights.encode('utf-8'), 0) ocr_meta = dn.load_meta(ocr_dataset.encode('utf-8')) imgs_paths = sorted(glob('%s/*lp.png' % output_dir)) print('Performing Character Recognition...') for i, img_path in enumerate(imgs_paths): print('\tScanning %s' % img_path) bname = basename(splitext(img_path)[0]) R, (width, height) = detect(ocr_net, ocr_meta, img_path.encode('utf-8'), thresh=ocr_threshold,
from darknet import darknet import cv2, numpy as np from ctypes import * net = darknet.load_net(b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/cfg/yolov3.cfg", b"C:/Users/bitcamp/anaconda3/Lib/site-packages/darknet/weight/yolov3.weights", 0) meta = darknet.load_meta(b"D:/darknet-master/build/x64/data/coco.data") cap = cv2.VideoCapture("C:/Users/bitcamp/Desktop/test4.mp4") print(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) while(cap.isOpened()): ret, image = cap.read() image = cv2.resize(image, dsize=(480, 640), interpolation=cv2.INTER_AREA) print(image.shape) if not ret: break frame = darknet.nparray_to_image(image) r = darknet.detect_image(net, meta, frame) boxes = [] for k in range(len(r)): width = r[k][2][2] height = r[k][2][3] center_x = r[k][2][0] center_y = r[k][2][1] bottomLeft_x = center_x - (width / 2) bottomLeft_y = center_y - (height / 2) x, y, w, h = bottomLeft_x, bottomLeft_y, width, height boxes.append((x, y, w, h)) for k in range(len(boxes)):
def to_box(r): boxes = [] scores = [] for rc in r: if rc[0] == b'text': cx, cy, w, h = rc[-1] scores.append(rc[1]) prob = rc[1] xmin, ymin, xmax, ymax = cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2 boxes.append([int(xmin), int(ymin), int(xmax), int(ymax)]) return boxes, scores import pdb if GPU: try: dn.set_gpu(GPUID) except: pass net = dn.load_net(yoloCfg.encode('utf-8'), yoloWeights.encode('utf-8'), 0) meta = dn.load_meta(yoloData.encode('utf-8')) os.chdir(pwd) def text_detect(img): r = detect_np(net, meta, img, thresh=0, hier_thresh=0.5, nms=None) ##输出所有box,与opencv dnn统一 bboxes = to_box(r) return bboxes
if __name__ == '__main__': try: input_dir = sys.argv[1] output_dir = input_dir lp_threshold = .25 lp_weights = 'data/lp-detector/lpd.weights' lp_netcfg = 'data/lp-detector/lpd.cfg' lp_dataset = 'data/lp-detector/lpd.data' lp_net = dn.load_net(lp_netcfg.encode('utf-8'), lp_weights.encode('utf-8'), 0) lp_meta = dn.load_meta(lp_dataset.encode('utf-8')) imgs_paths = glob('%s/*car.png' % input_dir) print('Searching for license plates...') for i, img_path in enumerate(imgs_paths): print('\t Processing %s' % img_path) bname = splitext(basename(img_path))[0] R, _ = detect(lp_net, lp_meta, img_path.encode('utf-8'), thresh=lp_threshold)
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)
def __init__(self, gpu, cfg, weights, data): darknet.set_gpu(gpu) self.net = darknet.load_net(str.encode(cfg), str.encode(weights), 0) self.meta = darknet.load_meta(str.encode(data))