def image_classification(image, network, class_names): width = darknet.network_width(network) height = darknet.network_height(network) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) darknet_image = darknet.make_image(width, height, 3) darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) detections = darknet.predict_image(network, darknet_image) predictions = [(name, detections[idx]) for idx, name in enumerate(class_names)] darknet.free_image(darknet_image) return sorted(predictions, key=lambda x: -x[1])
def Detector(): global metaMain, netMain, altNames configPath = "./yolov3.cfg" weightPath = "./yolov3.weights" metaPath = "./data/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("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 darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) for i in range(0, len(list_img)): path = './data/dataset/' + list_img[i] # detections = (darknet.performDetect(path, 0.25, configPath, weightPath, metaPath, False, False)) detections = darknet.detect(netMain, metaMain, path.encode("ascii"), 0.25) SuperResolution(list_img[i], preprocess(path, detections))
def YOLO(): #global pipeline, config #pipeline = rs.pipeline() #config = rs.config() #config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30) #pipeline.start(config) global metaMain, netMain, altNames configPath = "./cfg/yolov3-obj.cfg" weightPath = "yolo-obj_last.weights" metaPath = "./data/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("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 global darknet_image darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3)
def InitialiseYOLO(): print('YOLO Init') global metaMain, netMain, altNames, darknet_image #configPath = "./cfg/yolov4.cfg" configPath = "/var/darknet/cfg/yolov3.cfg" #weightPath = "./yolov4.weights" weightPath = "/var/darknet/cfg/yolov3.weights" #metaPath = "./cfg/coco.data" metaPath = "/var/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 darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) return netMain, metaMain
def Inference(img): frame_resized = cv2.resize( img, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) framebytes = frame_resized.tobytes() darknet.copy_image_from_bytes(darknet_image, framebytes) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.75) return detections
def image_detection(self, image_bgr, network, class_names, class_colors, thresh=0.25): # 判断输入图像是否为3通道 if len(image_bgr.shape) == 2: image_bgr = np.stack([image_bgr] * 3, axis=-1) # 获取原始图像大小 orig_h, orig_w = image_bgr.shape[:2] 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_bgr, 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) '''注意:这里原始代码依旧是608*608,而不是原图大小,因此我们需要转换''' new_detections = [] for detection in detections: pred_label, pred_conf, (x, y, w, h) = detection new_x = x / width * orig_w new_y = y / height * orig_h new_w = w / width * orig_w new_h = h / height * orig_h # 可以约束一下 (x1, y1, x2, y2) = self.bbox2point((new_x, new_y, new_w, new_h)) x1 = x1 if x1 > 0 else 0 x2 = x2 if x2 < orig_w else orig_w y1 = y1 if y1 > 0 else 0 y2 = y2 if y2 < orig_h else orig_h (new_x, new_y, new_w, new_h) = self.point2bbox((x1, y1, x2, y2)) new_detections.append( (pred_label, pred_conf, (new_x, new_y, new_w, new_h))) image = darknet.draw_boxes(new_detections, image_rgb, class_colors) return cv2.cvtColor(image, cv2.COLOR_RGB2BGR), new_detections
def convertBox(self, box, image_size): net_size = (dk.network_height(self.net), dk.network_width(self.net)) #计算在图片缩放时,填充了多大的边距框 nsize, top, bottom, left, right = YOLO.calScale(image_size, net_size) x = (box[0] - left) / nsize[0] * image_size[1] y = (box[1] - top) / nsize[1] * image_size[0] w = box[2] / nsize[0] * image_size[1] h = box[3] / nsize[1] * image_size[0] # x, y, w, h = [box[i] / net_size[i % 2] * image_size[(i+1) % 2] for i in range(len(box))] top = int(round(y - h / 2)) bottom = int(round(y + h / 2)) left = int(round(x - w / 2)) right = int(round(x + w / 2)) return top, bottom, left, right
def __init__(self,configPath,weightPath,metaPath): self.netMain = None self.metaMain = None self.altNames = None # global self.metaMain, netMain, self.altNames 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 self.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") self.altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass # out = cv2.VideoWriter( # "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 self.darknet_image = darknet.make_image(darknet.network_width(self.netMain), darknet.network_height(self.netMain),3)
def detect_yolo(self, frame_read): prev_time = time.time() frame_resized = cv2.resize(frame_read, (darknet.network_width( self.netMain), darknet.network_height(self.netMain)), interpolation=cv2.INTER_LINEAR) frame_rgb = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB) darknet.copy_image_from_bytes(self.darknet_image, frame_rgb.tobytes()) detections = darknet.detect_image(self.netMain, self.metaMain, self.darknet_image, thresh=0.35) #print(1/(time.time()-prev_time)) return detections
def predict_image(self, image, thresh=0.25, is_show=True, save_path=''): ''' :param image: cv2.imread 图像, darknet自己会对图像进行预处理 :param thresh: 置信度阈值, 其它阈值不变 :param is_show: 是否将画框之后的图像返回 :param save_path: 画框后的保存路径 :return: 返回1个矩阵 ''' # bgr->rgb rgb_img = image[..., ::-1] # 获取图片大小,网络输入大小 height, width = rgb_img.shape[:2] network_width = darknet.network_width(self.netMain) network_height = darknet.network_height(self.netMain) # 将图像resize到输入大小 rsz_img = cv2.resize(rgb_img, (network_width, network_height), interpolation=cv2.INTER_LINEAR) # 转成tensor的形式,以及[1,C,H,W] darknet_image, _ = darknet.array_to_image(rsz_img) detections = darknet.detect_image(self.netMain, self.metaMain, darknet_image, thresh=thresh) if is_show: for detection in detections: x, y, w, h = detection[2][0], \ detection[2][1], \ detection[2][2], \ detection[2][3] # 置信度 conf = detection[1] # 预测标签 label = detection[0].decode() # 获取坐标 x *= width / network_width w *= width / network_width y *= height / network_height h *= height / network_height # 转成x1y1x2y2,左上右下坐标; x是w方向 xyxy = np.array([x - w / 2, y - h / 2, x + w / 2, y + h / 2]) index = self.names.index(label) label_conf = f'{label} {conf:.2f}' self._plot_one_box(xyxy, rgb_img, self.colors[index], label_conf) bgr_img = rgb_img[..., ::-1] # 保存图像 if save_path: cv2.imwrite(save_path, bgr_img) return bgr_img #返回画框的bgr图像 return detections
def image_detection(image_path, network, class_names, class_colors, thresh): # 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) darknet.free_image(darknet_image) image = darknet.draw_boxes(detections, image_resized, class_colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
def prepare_batch(images, network, channels=3): width = darknet.network_width(network) height = darknet.network_height(network) darknet_images = [] for image in images: image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) custom_image = image_resized.transpose(2, 0, 1) darknet_images.append(custom_image) batch_array = np.concatenate(darknet_images, axis=0) batch_array = np.ascontiguousarray(batch_array.flat, dtype=np.float32)/255.0 darknet_images = batch_array.ctypes.data_as(darknet.POINTER(darknet.c_float)) return darknet.IMAGE(width, height, channels, darknet_images)
def count(self, image): """ 计数图片中出现的所有类别数目 返回值:字典 key-类别, value-数量 """ items_count = {} img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) img_resized = YOLO.letterbox_image( img_rgb, (dk.network_width(self.net), dk.network_height(self.net))) dk.copy_image_from_bytes(self.darknet_image, img_resized.tobytes()) detections = dk.detect_image(self.net, self.class_names, self.darknet_image, self.thresh) for detection in detections: pred_class, score, box = detection count = items_count.get(pred_class, 0) items_count[pred_class] = count + 1 return items_count
def image_detection(image, network, class_names, class_colors, thresh): 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 __init__( self, config_path: str = str(DEF_CONF, encoding="ascii"), weights_path: str = str(DEF_W, encoding="ascii"), data_path: str = str(DEF_DATA, encoding="ascii"), gpu_enabled: bool = True, ): self._config_path = config_path.encode("ascii") self._weights_path = weights_path.encode("ascii") self._data_path = data_path.encode("ascii") self._gpu_enabled = 0 if gpu_enabled else 1 dn.set_gpu(self._gpu_enabled) self._net = dn.load_net(self._config_path, self._weights_path, 0) self._metadata = dn.load_meta(self._data_path) self._darknet_image = dn.make_image(dn.network_width(self._net), dn.network_height(self._net), 3)
def prepare_batch(images, network, channels=3): # YOLO의 허용 크기: 320x320, 609X609, 416X416 width = darknet.network_width(network) height = darknet.network_height(network) darknet_images = [] for image in images: image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #opencv의 경우, image를 BGR 형태로 불러오므로 RGB로 변환 image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) custom_image = image_resized.transpose(2, 0, 1) #opencv의 경우, image를 HWC 형태로 불러오므로 CHW 형태로 변환 darknet_images.append(custom_image) batch_array = np.concatenate(darknet_images, axis=0) #data 배열 합치기 batch_array = np.ascontiguousarray(batch_array.flat, dtype=np.float32)/255.0 #memory에 연속적인 배열 생성 및 정규화 darknet_images = batch_array.ctypes.data_as(darknet.POINTER(darknet.c_float)) #data 형변환 return darknet.IMAGE(width, height, channels, darknet_images)
def get_img(frame_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.25) image = cvDrawBoxes(detections, frame_resized) print("get_iamging") image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) return image
def Detect_Image(img): darknet_image = darknet.make_image(netW, netH, 3) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_resized = cv2.resize( img_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image, img_resized.tobytes()) detections = darknet.detect_image(netMain, altNames, darknet_image, thresh=0.5) #print("ID_detections = " , id(detections)) return detections
def __init__(self, configPath="", weightPath="", classPath=""): 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(classPath): raise ValueError("Invalid classes file path `" + os.path.abspath(classPath) + "`") self.net = dk.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) self.class_names = YOLO.getClass(classPath) self.darknet_image = dk.make_image(dk.network_width(self.net), dk.network_height(self.net), 3) self.thresh = 0.5 self.colors = self.getColors()
def __init__(self, darknet, config_file, data_file, weight_file, threshold=0.7, **kwargs): self.network, self.class_names, self.class_colors = darknet.load_network( os.path.abspath(config_file), os.path.abspath(data_file), os.path.abspath(weight_file), batch_size=1) 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.threshold = threshold self.__dict__.update(kwargs)
def load_model(metaMain, netMain, altNames, version): if version == 1: configPath = "./cfg/yolov4.cfg" else: configPath = "./cfg/small_yolov4.cfg" weightPath = "./weights/yolov4.weights" metaPath = "./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 # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain), 3) return darknet_image, metaMain, netMain, altNames
def __init__(self): self.avm_img_sub = rospy.Subscriber("avm_usb_cam/image_raw", Image, self.image_callback) self.parking_cand_pub = rospy.Publisher('/parking_cands', PoseArray, queue_size=1) self.metaMain = None self.netMain = None self.altNames = None self.image = None configPath = "./yolo-obj_onlyFree.cfg" weightPath = "./yolo-obj_parking.weights" #ms: loading the weight file # weightPath = "./yolo-obj_low_dimension.weights"#ms: the size of input has to be (224,224) metaPath = "./obj.data" 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 self.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") self.altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass self.darknet_image = darknet.make_image( darknet.network_width(self.netMain), darknet.network_height(self.netMain), 3)
def get_classification(self, frame_read): prev_time = time.time() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (darknet.network_width(self.netMain), darknet.network_height(self.netMain)), interpolation=cv2.INTER_LINEAR) rows = frame_read.shape[0]/frame_resized.shape[0] cols = frame_read.shape[1]/frame_resized.shape[1] darknet.copy_image_from_bytes(self.darknet_image,frame_resized.tobytes()) detections = darknet.detect_image(self.netMain, self.metaMain, self.darknet_image, thresh=0.25) # print(detections) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # out.write(frame_resized) print(1/(time.time()-prev_time)) # cv2.imshow('Demo', image) if cv2.waitKey(3) & 0xFF == 27 : pass scores = [] classes = [] boxes = [] num_detections = len(detections) for detection in detections: x, y, w, h = detection[2][0],\ detection[2][1],\ detection[2][2],\ detection[2][3] xmin, ymin, xmax, ymax = convertBack(float(x), float(y), float(w), float(h)) pt1 = dict(x = int(xmin*cols), y = int(ymin*rows)) pt2 = dict(x = int(xmax*cols), y = int(ymax*rows)) boxes.append({'topleft' : pt1, 'bottomright' : pt2}) if detection[0].decode() == 'person': #is it person???? classes.append('1') else: classes.append('0') scores.append(detection[1]) # out.release() print(boxes, scores, classes, num_detections) return boxes, [scores], classes, num_detections
def image_detection_lime(image, network, class_names, class_colors, thresh): # Basically same as image_detection, but we get the image as a numpy array #print("entrou image detection LIME") 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, detection_prob_list = darknet.detect_image_lime(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 __init__(self, libPath: str, configPath: str, weightPath: str, metaPath: str, conf_threshold: float = 0.5, nms_threshold: float = 0.45, **kwargs): super().__init__() configPath = os.path.abspath(configPath) weightPath = os.path.abspath(weightPath) metaPath = os.path.abspath(metaPath) assert osp.exists( libPath ), "Invalid darknet library path `" + os.path.abspath(libPath) + "`" assert osp.exists( configPath ), "Invalid config path `" + os.path.abspath(configPath) + "`" assert osp.exists( weightPath ), "Invalid weight path `" + os.path.abspath(weightPath) + "`" assert osp.exists( metaPath), "Invalid meta path `" + os.path.abspath(metaPath) + "`" sys.path.append(os.path.abspath(os.path.dirname(libPath))) pwd = os.getcwd() os.chdir(os.path.abspath(os.path.dirname(libPath))) import darknet self.darknet = darknet self.netMain = self.darknet.load_net_custom(configPath.encode("ascii"), weightPath.encode("ascii"), 0, 1) self.metaMain = self.darknet.load_meta(metaPath.encode("ascii")) self.darknet_image = self.darknet.make_image( darknet.network_width(self.netMain), darknet.network_height(self.netMain), 3) os.chdir(pwd) self.conf_threshold = conf_threshold self.nms_threshold = nms_threshold self.input_size = (self.darknet.network_width(self.netMain), self.darknet.network_height(self.netMain))
def save_annotations(name, network, image, detections, class_names): """ Files saved with image_name.txt and relative coordinates """ image = cv2.imread(name) width = darknet.network_width(network) height = darknet.network_height(network) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) print('big_image ', image.shape) print('small_image ', image_resized.shape) # image = orig_image scale_x = image.shape[1] / image_resized.shape[1] scale_y = image.shape[0] / image_resized.shape[0] file_name = name.split(".")[:-1][0] + ".txt" print(file_name) with open(file_name, "w") as f: print(file_name, len(detections)) if (len(detections) == 0): f.write("0,0,0,0,0,0,-1,-1\n") else: for label, confidence, bbox in detections: left, top, right, bottom = darknet.bbox2points( bbox, scale_x, scale_y) x = left y = top w = right - left h = bottom - top # x, y, w, h = convert2relative(image, bbox) print(x, y, w, h, float(0.99), label, -1, -1) label = class_names.index(label) f.write( "{:.4f},{:.4f},{:.4f},{:.4f},{:.4f},{},{:n},{:n}\n".format( x, y, w, h, float(0.99), label, -1, -1))
def main(): image = cv2.imread('test/test2.jpg', cv2.COLOR_BGR2RGB) width = image.size().width height = image.size().height darknet_meta = darknet.load_meta('data/coco.data'.encode('ascii')) darknet_model = darknet.load_net_custom('cfg/yolov4.cfg'.encode('ascii'), 'yolov4.weights'.encode('ascii'), 0, 1) darknet_size = (darknet.network_width(darknet_model), darknet.network_height(darknet_model)) darknet_image = darknet.make_image(darknet_size[0], darknet_size[1], 3) image_result = yolo.detector(image, (width, height), darknet_model, darknet_meta, darknet_image, darknet_size, False)
def image_detection(image_path, network, class_names, class_colors, thresh): # 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(darkmask: 99% (left_x: 43 top_y: 28 width: 40 height: 39) mask: 100% (le< /home/fadi/다운로드/Yolo_mark/x64/Release/data/train.txt >ft_x: 134 top_y: 24 width: 32 height: 34) mask: 99% (left_x: 205 top_y: 20 width: 35 height: 41)net_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(self, image): # Darknet doesn't accept numpy images. # Create one with image we reuse for each detect width = darknet.network_width(self.network) height = darknet.network_height(self.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(self.network, self.class_names, darknet_image, thresh=self.threshold) detections = self.filter_classes( detections) # Filters out unwanted classes detections = self.convert2relative(image_resized, detections) return detections
def __init__(self): self.image_sub = rospy.Subscriber("/color1/image_raw", Image, self.callback) self.pub = rospy.Publisher('/tmp', Int32, queue_size=10) self.bridge = CvBridge() r = rospkg.RosPack() path = r.get_path('yolo') + '/config/libdarknet.so' self.config_path = r.get_path('yolo') + '/config/yolo-obj.cfg' self.weight_path = r.get_path('yolo') + '/config/yolo.weights' self.data_path = r.get_path('yolo') + '/config/obj.data' self.network, self.class_names, self.class_colors = darknet.load_network( self.config_path, self.data_path, self.weight_path, batch_size=1) self.thresh = 0.50 self.width = darknet.network_width(self.network) self.height = darknet.network_height(self.network) print('complete to load')
def YOLO(): global metaMain, netMain, altNames configPath = "./cfg/yolov3.cfg" weightPath = "./yolov3.weights" metaPath = "./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 #cap = cv2.VideoCapture(0) cap = cv2.VideoCapture("test.mp4") cap.set(3, 1280) cap.set(4, 720) out = cv2.VideoWriter( "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.25) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(1/(time.time()-prev_time)) cv2.imshow('Demo', image) cv2.waitKey(3) cap.release() out.release()