def linkDetect(imagePath): #image = cv2.imread(imagePath) yolo_result = performDetect(imagePath, configPath="./cfg/yolo-link.cfg", weightPath="./weights/yolo-link_final.weights", metaPath="./data/link.data", showImage=False, makeImageOnly=False, initOnly=False) coords = [-1, -1] width = -1 height = -1 highestConfidence = 0.0 for detected in yolo_result: if detected[0] == 'link' and detected[1] > highestConfidence: coords[0] = int(detected[2][0]) coords[1] = int(detected[2][1]) width = int(detected[2][2]) height = int(detected[2][3]) highestConfidence = detected[1] coords.extend(detectDistance(coords[0], coords[1], width, height)) # Return x and y of link bounding box as well as distance and offset angle of figurine return coords
def detector(jobs, imgds, flag, dn_width, dn_height): signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) logger(nameprefix="darknet") logging.info(str(os.getpid()) + " detector start") thresh = 0.5 # v1 # cfgpath = ".\\darknet_data\\yolov4-tra_416.cfg" # weipath = ".\\darknet_data\\yolov4-tra_320_best.weights" # metpath = ".\\darknet_data\\obj.data" #v8 cfgpath = ".\\darknet_data\\v8\\yolov4-tra_416.cfg" weipath = ".\\darknet_data\\v8\\yolov4-tra_416_best.weights" metpath = ".\\darknet_data\\v8\\obj.data" darknet.performDetect(thresh=thresh, configPath=cfgpath, weightPath=weipath, metaPath=metpath, initOnly=True) darknet_image = darknet.make_image(darknet.network_width(darknet.netMain), darknet.network_height(darknet.netMain), 3) dn_width.value = darknet.network_width(darknet.netMain) dn_height.value = darknet.network_height(darknet.netMain) flag.value = True while True: if (jobs.empty()): time.sleep(1) logging.debug("jobs.qisze() = {0} sleep(1)".format( str(jobs.qsize()))) else: #job.lat, job.lon, job.time, job.frame, job.frame_count # fps_count_start_time = time.time() job = jobs.get() logging.debug( "job.lat = {0} job.lon = {1} job.time = {2} job.frame.type = {3} job.frame_count = {4}" .format(job.lat, job.lon, job.time, type(job.frame), job.frame_count)) darknet.copy_image_from_bytes(darknet_image, job.frame.tobytes()) detections = darknet.detect_image(darknet.netMain, darknet.metaMain, darknet_image, thresh=thresh) job.detections = detections imgds.put(job)
def fetch_frame(): cv2.namedWindow('frame', cv2.WINDOW_NORMAL) while (True): img = getRGBFrame() cv2.imshow('frame', img) #no need but keep for sanity check detections = darknet.performDetect() if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows()
def getResult(path="LinkLaser/1.jpg"): result = performDetect(imagePath=path, configPath="./yolo-link.cfg", weightPath="./yolo-link_final.weights", metaPath="./link.data", showImage=False, makeImageOnly=False, initOnly=False) return result
def uploadFile(): """ REST API endpoint for image inference Returns: JSON Dictionary -- Return a dictionary containing image in base64 format and statistics of the counts and classification of rotifers """ file = request.files['file'] file.save("./test.jpg") stats = darknet.performDetect(imagePath="./test.jpg") with open("result.jpg", "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) base64_string = encoded_string.decode('utf-8') return jsonify(img=base64_string, stat=stats)
def getLinkCoords(img): yolo_result = performDetect(img, configPath="./yolo-link.cfg", weightPath="./yolo-link_final.weights", metaPath="./link.data", showImage=False, makeImageOnly=False, initOnly=False) coords = [-1, -1] for detected in yolo_result: if detected[0] == 'link': coords[0] = int(detected[2][0]) coords[1] = int(detected[2][1]) return coords
def default_yolo(): path = os.getcwd() imgpath = '/home/pi/Downloads/tug.jpg' configPath = "./cfg/yolov3_training.cfg" weightPath = "./cfg/yolov3_training_last.weights" metaPath= "./data/obj.data" os.chdir('/home/pi/darknet') from darknet import performDetect a = performDetect(imagePath=imgpath, configPath=configPath, weightPath=weightPath, metaPath=metaPath) os.chdir(path) return a
def default_yolo(): path = os.getcwd() imgpath = '/home/pi/Documents/flask_webcam/image.jpg' output_path = '/home/pi/Documents/flask_webcam/static/prediction.jpg' configPath = "./cfg/yolov3_training.cfg" weightPath = "./cfg/yolov3_training_last.weights" metaPath= "./data/obj.data" os.chdir('/home/pi/darknet') from darknet import performDetect a = performDetect(imagePath=imgpath, configPath=configPath, weightPath=weightPath, metaPath=metaPath) os.system('cp -f ./predictions.jpg '+output_path) os.chdir(path) return a
def check_folder(): original_dir = '/media/huxi/DATA/inf_master/Semester-5/Thesis/code/dataset/valid' restore_dir = '/media/huxi/DATA/inf_master/Semester-5/Thesis/code/YOLO_dataset_one_class/valid' #class_table_list = ['BLO','BLR','BOO','BOR','OLO','OLR','OOO','OOR'] ori_alllist = os.listdir(original_dir) for folder in ori_alllist: class_res_folder = os.path.join(restore_dir, folder) if (not os.path.exists(class_res_folder)): print('create new folder') os.makedirs(class_res_folder) class_ori_folder = os.path.join(original_dir, folder) for frames_folder in os.listdir(class_ori_folder): ori_frames_folder = os.path.join(class_ori_folder, frames_folder) res_frames_folder = os.path.join(class_res_folder, frames_folder) #print(res_frames_folder) if (not os.path.exists(res_frames_folder)): print('create new folder: frames') os.makedirs(res_frames_folder) for img_name in os.listdir(ori_frames_folder): new_img_name = os.path.join(res_frames_folder, img_name) if (os.path.exists(new_img_name)): print('skip') continue ori_img_name_path = os.path.join(ori_frames_folder, img_name) #print(ori_img_name_path) detections = performDetect( imagePath=ori_img_name_path, thresh=0.25, configPath="yolov4-taillight.cfg", weightPath= "/home/huxi/YOLO_v4/darknet/backup/yolov4-taillight_2000.weights", metaPath="data/taillights.data") img_with_boxes = detections['image'] img = Image.fromarray(img_with_boxes, 'RGB') #print(new_img_name) img.save(new_img_name)
model="models/yolov4.weights" # yolo9000 # data="darknet/cfg/combine9k.data" # config="darknet/cfg/yolo9000.cfg" # model="models/yolo9000.weights" # yolo-openimages # data="models/yolo_open_images/yolo.data" # config="models/yolo_open_images/yolov3-spp.cfg" # model="models/yolo_open_images/yolov3-spp_final.weights" threshold=0.25 outimg = indir.replace('images','images_predict')+"/"+filename.replace('.png','.jpg') out = darknet.performDetect(imagePath=indir+'/'+filename, thresh= threshold, configPath = config, weightPath = model, metaPath=data, showImage= True, makeImageOnly = True, initOnly= False,outImage=outimg,includeClasses=countItems) counts={} for i in countItems: counts[i] = 0 for i in out: if i[0] in countItems: counts[i[0]] += 1 results[filename] = counts with open("results.json", "w") as myfile: myfile.write("{}\n".format(json.dumps(results, indent=4, sort_keys=True)))
import darknet as dn from tqdm import tqdm import os image_dir = r"..\Input\train_jpg_0" arr = os.listdir(image_dir) %%time image_class = [] for f in tqdm(arr): full_image_path = os.path.join(image_dir, f) # print (f, full_image_path) classes = dn.performDetect(imagePath=full_image_path, thresh= 0.25, configPath = "cfg/yolov3.cfg", weightPath = "yolov3.weights", metaPath= "./data/coco.data", showImage= False, makeImageOnly = False, initOnly= False) image_class.append([f, classes]) image_dir = r"..\Input\train_jpg_1" arr = os.listdir(image_dir) for f in tqdm(arr): full_image_path = os.path.join(image_dir, f) # print (f, full_image_path) classes = dn.performDetect(imagePath=full_image_path, thresh= 0.25, configPath = "cfg/yolov3.cfg", weightPath = "yolov3.weights",
def yolo_crop(image_path, output_path, pottery_labels, yolo_weight_path, detect_thresh=0.5, min_pixels=256, make_square=False, new_dims=None): ''' Function to perform the detection and cropping of images in a given folder. Assumes it is being run from darknet/build/x64 folder with darknet.exe and DLL's there Parameters: --------------------- image_path: str Path to the folder where the images to be cropped are output_path: str Path to where the cropped images should be saved, can be same as image_path for quick testing pottery_labels: list of str The names of the categories that you want to save from YOLO, i.e. ['mug', 'cup'] detect_thresh: float (default= 0.5) The detection threshold for YOLO, mugs worked well with 0.65 min_pixels: int The minimum resolution of the output image in X and Y, default 256, helps stop very small images being saved make_square: Bool Resize image to make square? If true and new_dims aren't set, resizes to square using smallest dimension of original image. Otherwise makes square with new dimensions. new_dims: Tuple New dimensions for image if make_square=True ''' # dataframe to store detected object details so this doesn't need to be run again image_details = pd.DataFrame(index=[0], columns=[ 'originalFileName', 'newFileName', 'label', 'confidence', "imageHeight", "imageWidth", "xPosition", "yPosition", "croppedHeight", "croppedWidth" ]) #tracking variables start_time = time.time() images_processed = 0 for image in glob.iglob(image_path): detected = performDetect(imagePath=image, weightPath=yolo_weight_path, showImage=False, thresh=detect_thresh) #loads the image to be cropped try: img = cv2.imread(image) imgHeight, imgWidth = img.shape[:2] except: print("Error reading file", image) next #goes over all detected objects and crops then saves the cropped image for object in detected: label = object[0] confidence = object[1] bounds = object[2] print(f"{label} {confidence}") #box dimensions width = int(bounds[2]) height = int(bounds[3]) xCoord = int(bounds[0] - width / 2) yCoord = int(bounds[1] - height / 2) #padding around the detected boundary box set at 10% border = int(width * 0.1) #only process/save images that are large enough, reduces small snippets of objects if (label in pottery_labels) and (width > min_pixels) and ( height > min_pixels): #if object is detected in corner of image, don't apply border if (xCoord < border): print("Object in X corner") xCoord = 0 border = 0 if (yCoord < border): print("Object in Y corner") yCoord = 0 border = 0 print( f"X:{xCoord} Y:{yCoord} width:{width} height:{height} border:{border}" ) if make_square: if new_dims == None: new_dimension = (min(height, width), min(height, width)) else: new_dimension = new_dims #Crop to box, get the smallest dimension and interpolate to make square with smallest dimension for height and width. crop_img = img[yCoord - border:yCoord + height + border, xCoord - border:xCoord + width + border] crop_img = cv2.resize(crop_img, new_dimension, interpolation=cv2.INTER_AREA) else: crop_img = img[yCoord - border:yCoord + height + border, xCoord - border:xCoord + width + border] cropHeight, cropWidth = crop_img.shape[:2] #cv2.imshow("cropped", crop_img) #Uncomment to show the cropped images #cv2.waitKey(0) original_file = os.path.basename(os.path.splitext(image)[0]) file_name = f"{original_file}_{label}_{confidence:0.2f}.jpg" cv2.imwrite(os.path.join(output_path, file_name), crop_img) image_details.loc[image_details.index.max() + 1] = [ original_file, file_name, label, confidence, imgHeight, imgWidth, xCoord, yCoord, cropHeight, cropWidth ] #increment how may images were processed images_processed = images_processed + 1 processing_time = time.time() - start_time #save off the image details to the same folder as the cropped images once all images completed print( f"{images_processed:2.0f} Images Processed in {processing_time:3.1f} seconds" ) image_details.to_csv(os.path.join(output_path, "CroppedImageDetails.csv"), index=False)
def predict(self): return darknet.performDetect("test.jpg")
def doDetect(): images = [] with open('./data/test.txt', 'r') as imageList: images = imageList.readlines() imlocs = [[20, 0], [16, -25], [19, -15], [17, -5], [25, 0], [30, 0], [20, 15], [27, 25]] count = 0 totalTime = 0 d_offset_avg = 0 a_offset_avg = 0 while True: startTime = time.time() if count is len(images): break curr = images[count].strip('\n') yolo_result = performDetect( curr, configPath="./cfg/yolo-glue.cfg", weightPath="./weights/yolo-glue_final.weights", metaPath="./data/glue.data", showImage=False, makeImageOnly=False, initOnly=False) coords = [-1, -1] detect_loc = [-1, -1] width = -1 height = -1 highestConfidence = 0.0 for detected in yolo_result: if detected[0] == 'glue' and detected[1] > highestConfidence: coords[0] = int(detected[2][0]) coords[1] = int(detected[2][1]) width = int(detected[2][2]) height = int(detected[2][3]) highestConfidence = detected[1] detect_loc = detectDistance(coords[0], coords[1], width, height) elapsed = time.time() - startTime d_offset = abs(float(detect_loc[0]) - imlocs[count][0]) a_offset = abs(float(detect_loc[1]) - imlocs[count][1]) print( "File: {}, Coords: {}, Distance offset: {}, Angle offset: {}, Elapsed: {}" .format(curr, coords, d_offset, a_offset, elapsed)) count = count + 1 totalTime = totalTime + elapsed d_offset_avg = d_offset_avg + d_offset a_offset_avg = a_offset_avg + a_offset print( "Average time: {}, Average Distance offset: {}, Average Angle offset: {}" .format(totalTime / count, d_offset_avg / count, a_offset_avg / count))
import darknet if __name__ == "__main__": darknet.performDetect(imagePath="data/dog.jpg")
def loop(self): r = rospy.Rate( self.max_detection_rate ) # Hz to check if there is a new detection being requested while not rospy.is_shutdown(): if self.current_image_msg is not None: # copy to prevent being changed from the callback img_msg = deepcopy(self.current_image_msg) self.current_image_msg = None if not self.using_compressed: try: image = self.bridge.imgmsg_to_cv2(img_msg, "bgr8") except CvBridgeError as e: rospy.logerr(e) return else: # compressed, cant use cv_bridge in python # get image from topic np_arr = np.fromstring(img_msg.data, np.uint8) # get OpenCV version (major_version, _minor_version, _) = cv2.__version__.split(".") if major_version >= 3: image = cv2.imdecode(np_arr, cv2.IMREAD_COLOR) else: image = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR) publish_image = self.image_pub.get_num_connections() > 0 initOnly = False detections = performDetect(image, self.threshold, self.cfg_file, self.weights_file, self.data_file, initOnly) recog_obj_array = RecognizedObjectArray() image_h, image_w, _channels = image.shape if self.show_only_best_detection: best_object = None best_confidence = -1 best_obj_details = -1 for obj_id, detection in enumerate(detections): data = detection.split() class_name, confidence = data[:2] x_center, y_center, width, height = [ int(float(val)) for val in data[2:] ] confidence = float(confidence) # draw one detection on the image to be published if publish_image: color = (255, 0, 0) # blue image = self.draw_info(image, class_name, confidence, x_center, y_center, width, height, color) if self.debug: rospy.loginfo( '''\n''' '''class_name: %s\n''' '''confidence: %s\n''' '''x_center: %s\n''' '''y_center: %s\n''' '''width: %s\n''' '''height: %s''', class_name, confidence, x_center, y_center, width, height) recog_obj = RecognizedObject() recog_obj.class_name = class_name recog_obj.confidence = confidence x_offset = min(max(0, x_center - width / 2), image_w) y_offset = min(max(0, y_center - height / 2), image_h) recog_obj.bounding_box = RegionOfInterest( x_offset=x_offset, y_offset=y_offset, width=min(width, image_w - x_offset), height=min(height, image_h - y_offset)) if self.debug: rospy.loginfo("recog_obj.bounding_box") rospy.loginfo(recog_obj.bounding_box) if self.show_only_best_detection: if confidence > best_confidence: best_object = recog_obj best_confidence = confidence best_obj_details = [ class_name, confidence, x_center, y_center, width, height ] else: recog_obj_array.objects.append(recog_obj) if self.show_only_best_detection and best_object is not None: recog_obj_array.objects.append(best_object) class_name, confidence, x_center, y_center, width, height = tuple( best_obj_details) color = (0, 255, 0) # green image = self.draw_info(image, class_name, confidence, x_center, y_center, width, height, color) recog_obj_array_stamped = RecognizedObjectArrayStamped() recog_obj_array_stamped.header = img_msg.header recog_obj_array_stamped.objects = recog_obj_array self.result_publisher.publish(recog_obj_array_stamped) # actually publish the image if publish_image: # Create CompressedImage and publish msg = CompressedImage() msg.header = img_msg.header msg.format = "jpeg" msg.data = np.array(cv2.imencode('.jpg', image)[1]).tostring() # Publish new image self.image_pub.publish(msg) r.sleep()
def main(data_file, cfg_file, weights_file, img_dir): imgs = os.listdir(img_dir) if imgs == []: return for img in tqdm.tqdm(imgs): # start = time.time() try: detections, shape = dn.performDetect( # imagePath=join(img_dir, img), image=join(img_dir, img), configPath=cfg_file, weightPath=weights_file, metaPath=data_file, showImage=False, ) # print('Detection done in %f s' % (time.time() - start)) except Exception as e: print('Warning! Detection failed. {}'.format(e)) continue # print(detections) #if detections == []: # nodet_dir = join(img_dir, 'no_det') # if not osp.exists(nodet_dir): # os.makedirs(nodet_dir) # shutil.move(join(img_dir, img), join(nodet_dir, img)) # continue E = objectify.ElementMaker(annotate=False) anno_tree = E.anntation( E.folder(img_dir), E.filename(img), E.path(join(img_dir, img)), E.source(E.database('HHD')), E.size( E.width(shape[0]), E.height(shape[1]), # E.depth(shape[2]) E.depth(3)), E.segmented(0), ) # E2 = objectify.ElementMaker(annotate=False) for det in detections: name = det[0] if name != 'person': continue # (x, y, w, h) xmin, ymin, xmax, ymax = reverse_convert(det[2], shape[1], shape[0]) # print(xmin, ymin, xmax, ymax) object_tree = E.object( E.name(name), E.pose('Unspecified'), E.truncated(0), E.difficult(0), E.bndbox(E.xmin(xmin), E.ymin(ymin), E.xmax(xmax), E.ymax(ymax))) anno_tree.append(object_tree) etree.ElementTree(anno_tree).write(join(img_dir, osp.splitext(img)[0] + ".xml"), pretty_print=True)
# Parameters imagePath = "/home/amusaal/DATA/kitchen.jpg" configPath = "/home/amusaal/darknetAB/cfg/yolov2.cfg" weightPath = "/home/amusaal/darknetAB/yolov2.weights" metaPath = "/home/amusaal/darknetAB/cfg/coco.data" showImage = False makeImageOnly = False initOnly = False # img = cv2.imread(imagePath) thresh = 0.25 MODE = 'TEST' # or 'WRITE / 'MEASURE' # Initialize network if MODE != 'READ': darknet.performDetect(initOnly=True) if MODE == 'MEASURE': T = [] for i in range(50): t_start = time() _ = darknet.performDetect() t_end = time() T.append(t_end - t_start) print('Inference time: {} s'.format(np.array(T).mean())) sys.exit(0) if MODE == 'TEST': # Load image img = cv2.imread('../DATA/Cornell/kitchen/data_04-22-13/rgbjpg/0001.jpg') cv2.imwrite("test/original.jpg", img)
detections = darknet.performDetect() if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows() def getRGBFrame(): frame_data = frame_socket.recv() buf = bytes(frame_data) array1D = np.frombuffer(buf[TIMESTAMP_BYTES:], dtype=np.uint8) img = np.reshape(array1D, (720, 1280, 3), order='C') timestamp = struct.unpack('Q', buf[0:TIMESTAMP_BYTES])[0] return img, timestamp def convert_msg(detections): return None def publish_probs(msg): framechange_socket.send(FRAME_TOPIC + SEP + msg) detections = darknet.performDetect() print(detections) def main(): #pull frames from the camera using cv #darknet.performdetect on cam image fetch_frame() #
import darknet as dn if __name__ == "__main__": #this main methood parses in the nn files from a user input and runs the object detection on an image #these lines read in the network files from the comandline but examples are comeented out below config = input('input the location of the config file \n') #config = "/home/spencer/darknet2/darknet/tsc3/tsc3.cfg" #nn config file weights = input('input the location of the weights file\n') #weights = "/home/spencer/darknet/tsc3/Backup/tsc3_50000.weights" #weights file data = input("input the location of the data file\n") #data = "/home/spencer/darknet2/darknet/tsc3/tsc3.data" #data file cont = True while(cont): path = input("input an image path\n") r = dn.performDetect(path,.25,config,weights,data) #perform the detection if r: print("detected toilet seat covers\n") print(r) #r is a list of tuples else: print("no object found\n")
def __init__(self): darknet_bin_dir_param = rospy.get_param("/darknet_bin_dir") self.camera_rostopic = rospy.get_param("~input_topic") self.data_file = rospy.get_param("~data_file", "cfg/coco.data") self.cfg_file = rospy.get_param("~cfg_file", "cfg/yolov3.cfg") self.weights_file = rospy.get_param("~weights_file", "yolov3.weights") self.threshold = rospy.get_param("~threshold", 0.25) self.debug = rospy.get_param("~debug", False) self.show_only_best_detection = rospy.get_param( "~show_only_best_detection", False) self.max_detection_rate = rospy.get_param("~max_detection_rate", 100) # Check if using compressed images, from the topic itself namespace_splits = self.camera_rostopic.split('/') if 'compressed' in namespace_splits: rospy.loginfo('Using compressed images') self.using_compressed = True else: self.using_compressed = False # Create CvBridge if using raw transport if not self.using_compressed: self.bridge = CvBridge() # Check if files exist darknet_bin_dir = os.path.expanduser(darknet_bin_dir_param) self.check_if_exists(darknet_bin_dir) darknet_bin_path = os.path.join(darknet_bin_dir, "darknet") self.check_if_exists(darknet_bin_path) cfg_file_full_path = os.path.join(darknet_bin_dir, self.cfg_file) self.check_if_exists(cfg_file_full_path) weights_file_full_path = os.path.join(darknet_bin_dir, self.weights_file) self.check_if_exists(weights_file_full_path) # Subscriber and Publisher rostopics subscriber_status_cbs = DarknetRosPy.SuperSubscribeListener(self) self.image_pub = rospy.Publisher( "~detection_image/compressed", CompressedImage, queue_size=1, subscriber_listener=subscriber_status_cbs) self.result_publisher = rospy.Publisher( "~detections", RecognizedObjectArrayStamped, queue_size=10, subscriber_listener=subscriber_status_cbs) # Class variables self.subscriber = None self.current_image_msg = None # Change directory to darknet os.chdir(darknet_bin_dir) # check if camera topic is in the rostopic list rostopic_list = subprocess.check_output(['rostopic', 'list']) if self.camera_rostopic not in rostopic_list: rospy.logwarn("Input topic {} not found yet".format( self.camera_rostopic)) # init darknet detections = performDetect(self.current_image_msg, self.threshold, self.cfg_file, self.weights_file, self.data_file, True) rospy.loginfo( "Darknet is ready. It will subscribe to images and process them as soon as there is a subscriber to the detections or detection image topic" )
img = cv2.imread(img_name) if type(img) == type(None): print('%s read failed' % (img_name)) continue # draw gt boxes # groundtruth_results = readAnnotations(xml_name) # draw_gt_rect(img, groundtruth_results) # detect result # detect_results, _ = dn.performDetect(image=img_name, detect_results, _ = dn.performDetect(image=img, thresh=confidence, hier_thresh=iou_threshold, nms=nms_threshold, configPath=args.cfg_file, weightPath=args.weights_file, metaPath=args.data_file, showImage=False) if len(detect_results) == 0: # print('%s:%s' % ("Do not detect any sku in the image" , os.path.basename(img_name))) # shutil.copyfile(xml_name , osp.join(no_detect_dir, os.path.basename(xml_name))) # shutil.copyfile(img_name , osp.join(no_detect_dir, os.path.basename(img_name))) cv2.imwrite(osp.join(no_detect_dir, osp.basename(img_name)), img) continue draw_det_detect(img, detect_results) # put it here # draw gt boxes
data = f.readlines() data = [x.strip() for x in data] # count the number of right and wrong classifications and # also record our ground truth and infered classes so that # we can perform analysis on the data later. right = 0 wrong = 0 y_true = [] y_pred = [] t0 = time.time() for image in data: res = performDetect(imagePath=image, configPath="data/yolov3-414.cfg", metaPath="data/414.data", weightPath="yolov3-414_final.weights", showImage=False) cnt = Counter([x[0] for x in res]) detections = sorted(list(cnt.items()), key=lambda x: x[1], reverse=True) classification = 'none' if len(detections) > 0 and detections[0][1] >= VOTING_THRESHOLD: classification = detections[0][0] if DEBUG: print('class:', classification) # verify whether the class is correct actual_class = 'none' labels = ".".join(image.split('.')[:-1]) + ".txt" with open(labels) as f:
from darknet import performDetect from glob import glob import os files = glob('pothole_weights/data/*.jpg') OUT_DIR = './pothole_weights/out' performDetect('', 0.1, "pothole_weights/yolo-pothole-test.cfg", "pothole_weights/yolo-pothole-train_10000.weights", "./cfg/obj.data", initOnly=True) for file in files: print("Read ", file) base_name = os.path.basename(file) out_path = os.path.join(OUT_DIR, base_name) result = performDetect(file, 0.1, "pothole_weights/yolo-pothole-test.cfg", "pothole_weights/yolo-pothole-train_10000.weights", "./cfg/obj.data", saveImage=out_path)
from darknet import performDetect from glob import glob import os cfg = "pothole_weights/pothole-yolov3-tiny.cfg" weight = "pothole_weights/pothole-yolov3-tiny.weights" meta = "./cfg/obj.data" result = performDetect('', 0.3, cfg, weight, meta, initOnly=True) files = glob('pothole_weights/data/*.jpg') OUT_DIR = './pothole_weights/out' for file in files: base_name = os.path.basename(file) out_path = os.path.join(OUT_DIR, base_name) result = performDetect(file, 0.3, cfg, weight, meta, saveImage=out_path) print("Saved in {}, result= {}".format(out_path, result))
lit = cv2.LINE_AA if lineType is None else lineType (tw, th) = cv2.getTextSize(text, fontFace, fontScale=fontScale, thickness=thn)[0] tx0 = org[0] ty0 = org[1] box_coords = ((tx0, ty0 + 2), (tx0 + tw - 2, ty0 - th - 2)) cv2.rectangle(img, box_coords[0], box_coords[1], rectangle_bgr, cv2.FILLED) cv2.putText(img, text, (tx0, ty0), fontFace, fontScale=fontScale, color=(0, 0, 0), thickness=1, lineType=lit) if __name__ == '__main__': imagePath = 'misica_azuki/IMG_3549.jpg' detections = darknet.performDetect( imagePath=imagePath, configPath='cfg/yolov3-tiny.cfg', weightPath='weights/yolov3-tiny.weights', metaPath='cfg/coco.data', showImage=True) # print(detections) # overlay detections on the image overlay_detections(detections)
for image_path, mask_path in zip(images_path, masks_path): img = cv2.imread(image_path) mask = cv2.imread(mask_path, 0) ret, mask = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY) label_img = label(mask) regions = regionprops(label_img)[0] minr, minc, maxr, maxc = regions.bbox quad_image = img[minr:maxr, minc:maxc, :] quad_h, quad_w = quad_image.shape[0], quad_image.shape[1] cv2.imwrite('tempo.png', quad_image) raw_detections = performDetect(imagePath='tempo.png', thresh=0.15, configPath=configPath, weightPath=weightPath, metaPath=metaPath, showImage=showImage_flag) xmlfile = image_path[:-3] + 'xml' if showImage_flag: detections = raw_detections['detections'] # detections2xml(detections['detections'], xmlfile, img, minr,minc,maxr,maxc) # plt.savefig('quad.png') else: detections = raw_detections detections2xml(detections, xmlfile, img, minr, minc, maxr, maxc) print(image_path)