def drawing(self, flag=0): random.seed(3) # deterministic bbox colors # video = self.set_saved_video(self.cap, self.output_video, (self.width, self.height)) if flag: self.draw() else: while self.cap.isOpened(): frame_resized = self.frame_queue.get() detections = self.detections_queue.get() # ------------------------------------------- # face_detections = self.face_detections_queue.get() # ------------------------------------------- fps = self.fps_queue.get() if frame_resized is not None: image = darknet.draw_boxes(detections, frame_resized, self.class_colors) # ------------------------------------------- # face_image = self.frame_queue_face.get() # face_image = darknet.draw_faces(face_detections, face_image, (252, 240, 2)) # ------------------------------------------- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # if self.output_video is not None: # video.write(image) self.drew_images.put(image) # ------------------------------------------- # self.face_image_queue.put(face_image) # cv2.imshow('face recognizer', face_image) # ------------------------------------------- if cv2.waitKey(fps) == 27: break # video.release() cv2.destroyAllWindows()
def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (width, height)) while cap.isOpened(): # ret, orignal_frame = cap.read() frame_resized = frame_queue.get() orignal_frame = orig_frame_queue.get() detections = detections_queue.get() # fps = fps_queue.get() if frame_resized is not None: # print('rfr', frame_width, frame_height) image = darknet.draw_boxes(detections, frame_resized, orignal_frame, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # print(image.shape) # image = cv2.resize(image, (1056, 720)) if args.out_filename is not None: video.write(image) if not args.dont_show: cv2.imshow('Inference', image) if cv2.waitKey(1) == 27: break cap.release() video.release() cv2.destroyAllWindows()
def drawing(frame_queue, detections_queue, fps_queue, curruntPointList, buffer_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (width, height)) while cap.isOpened(): frame_resized = frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() if frame_resized is not None: ## loop every box: print("\n\n\n\n\n>>>my data process begin here:") curruntPointList.clear() for detection in detections: point = POINT(detection[2][0], detection[2][1], detection[2][2] * detection[2][3]) curruntPointList.append(point) # print("\n" + str(detection[2][0])) ## loop end & point processing pointProcessing(curruntPointList, buffer_queue) ########################### image = darknet.draw_boxes(detections, frame_resized, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.out_filename is not None: video.write(image) if not args.dont_show: cv2.imshow('Inference', image) if cv2.waitKey(fps) == 27: break cap.release() video.release() cv2.destroyAllWindows()
def get_frame(frame_resized, detections): if frame_resized is not None: frame = darknet.draw_boxes(detections, frame_resized, class_colors) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes() return frame
def drawbox(img, bboxes): rdict, image, image_resized, predictor = Detector(img, bboxes) types = [] if len(rdict["vests"]) > 0: x11, y11, x21, y21 = rdict["vests"] x11, y11, x21, y21 = box_float2int(x11, y11, x21, y21) types.append(["SP1003"]) if len(rdict["nomask"]) > 0: x12, y12, x22, y22 = rdict["nomask"] x12, y12, x22, y22 = box_float2int(x12, y12, x22, y22) types.append(["SP1001"]) # if len(rdict["mask"]) > 0 : # x13,y13,x23,y23 = rdict["mask"] # x13 = int(x13) # y13 = int(y13) # x23 = int(x23) # y23 = int(y23) # cv2.rectangle(image, (x13, y13), (x23, y23), (0, 0, 255), 2) # types.append(["SP1009"]) image = darknet.draw_boxes(predictor, image_resized, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) return image, types
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) new_dets = [] for det in detections: new_x1 = det[2][0] * image.shape[1] / width new_y1 = det[2][1] * image.shape[0] / height new_x2 = det[2][2] * image.shape[1] / width new_y2 = det[2][3] * image.shape[0] / height new_det = (new_x1, new_y1, new_x2, new_y2) new_dets.append((det[0], det[1], new_det)) darknet.free_image(darknet_image) image = darknet.draw_boxes(new_dets, image, class_colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), new_dets
def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (width, height)) while cap.isOpened(): try: frame_resized = frame_queue.get(timeout=1) detections = detections_queue.get(timeout=1) fps = fps_queue.get() if frame_resized is not None: # detect and draw lines bgr = cv2.cvtColor(frame_resized, cv2.COLOR_RGB2BGR) res = imageProcessing(bgr) frame_resized = cv2.cvtColor(res, cv2.COLOR_BGR2RGB) image = darknet.draw_boxes(detections, frame_resized, class_colors, width, height) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) if args.out_filename is not None: video.write(image) if not args.dont_show: cv2.namedWindow('Inference', cv2.WINDOW_NORMAL) cv2.imshow('Inference', image) if cv2.waitKey(1) == 27: break except: cap.release() video.release() cv2.destroyAllWindows() return
def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) video = set_saved_video(cap, args.out_filename, (original_width, original_height)) nth_frame = 0 while cap.isOpened(): print("processing frame", nth_frame) frame = frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() if frame is not None: image = darknet.draw_boxes(detections, frame.copy(), class_colors) #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.out_filename is not None: video.write(image) if args.output_prefix is not None and len(detections) != 0: cv2.imwrite( args.output_prefix + str(nth_frame) + "_inference.jpg", image) cv2.imwrite(args.output_prefix + str(nth_frame) + ".jpg", frame) nth_frame += 1 if not args.dont_show: cv2.imshow('Inference', image) if cv2.waitKey(fps) == 27: break cap.release() video.release() cv2.destroyAllWindows()
def batch_detection(network, images, class_names, class_colors, thresh=0.25, hier_thresh=.5, nms=.45, batch_size=4): image_height, image_width, _ = check_batch_shape(images, batch_size) darknet_images = prepare_batch(images, network) batch_detections = darknet.network_predict_batch(network, darknet_images, batch_size, image_width, image_height, thresh, hier_thresh, None, 0, 0) batch_predictions = [] for idx in range(batch_size): num = batch_detections[idx].num detections = batch_detections[idx].dets if nms: darknet.do_nms_obj(detections, num, len(class_names), nms) predictions = darknet.remove_negatives(detections, class_names, num) images[idx] = darknet.draw_boxes(predictions, images[idx], class_colors) batch_predictions.append(predictions) darknet.free_batch_detections(batch_detections, batch_size) return images, batch_predictions
def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (width, height)) while cap.isOpened(): frame_resized = frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() if frame_resized is not None: image = darknet.draw_boxes(detections, frame_resized, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #預設剪中間背景當人臉 face = image[160:390, 200:430] #如果有人臉座標就取代預設背景 if detections: for label, confidence, bbox in detections: left, top, right, bottom = darknet.bbox2points(bbox) face = image[top:bottom, left:right] #if args.out_filename is not None: #video.write(image) if not args.dont_show: cv2.imshow('Inference', image) cv2.imshow('face', face) if cv2.waitKey(fps) == 27: break cap.release() video.release() cv2.destroyAllWindows()
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) image = darknet.draw_boxes(detections, image_resized, class_colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections if class_names == "fresh": fresh = 0 if class_names == "ripe": ripe = 0 if class_names == "raw": raw = 0 if class_names == "flowering": flowering = 0 if class_names == "alternaria": alternaria = 0 if class_names == "cedar": cedar = 0 if class_names == "fire-blight": fire_blight = 0 if class_names == "leaf-roller": leaf_roller = 0 if class_names == "fungal": fungal = 0
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) original_width = image.shape[1] original_height = image.shape[0] x_scale = original_width / width y_scale = original_height / height image_rgb = cv2.cvtColor(image.copy(), 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) if len(detections) == 0: return image, detections temp_detections = list() for i in range(len(detections)): x_detection = detections[i][2][0] * x_scale y_detection = detections[i][2][1] * y_scale w_detection = detections[i][2][2] * x_scale h_detection = detections[i][2][3] * y_scale temp_detections.append([detections[i][0], detections[i][1], (x_detection, y_detection, w_detection, h_detection)]) detections = temp_detections image = darknet.draw_boxes(detections, image, class_colors) return image, detections
def drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (darknet_width, darknet_height)) while cap.isOpened(): frame = frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() detections_adjusted = [] if frame is not None: for label, confidence, bbox in detections: bbox_adjusted = convert2original(frame, bbox) detections_adjusted.append( (str(label), confidence, bbox_adjusted)) image = darknet.draw_boxes(detections_adjusted, frame, class_colors) if not args.dont_show: cv2.imshow('Inference', image) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.out_filename is not None: video.write(image) if cv2.waitKey(fps) == 27: break cap.release() video.release() cv2.destroyAllWindows()
def draw_detections(self, detections, frame, convert_to_rgb=True, classes=[]): """ Draw the detections detected on the frame. Args: detections (list): All the detections in this format [(class, conf, [x, y, w, h]), ...]. frame (numpy.ndarray): The frame to draw the detections on. convert_to_rgb (bool, optional): Whether to convert the frame to RGB. Defaults to True. classes (list, optional): Limit the detections to these class names. Defaults to []. Returns: numpy.ndarray: The given frame with detections (BBs) on it. """ if classes != []: for i, det in enumerate(detections): class_name = det[0] if class_name not in classes: del detections[i] image = darknet.draw_boxes(detections, frame, self.class_colors) if convert_to_rgb: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) return image
def callback(self, msg): begin = time.clock() frame = self.bridge.imgmsg_to_cv2(msg, "bgr8") frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) #cv2.imshow("frame_rgb", frame_rgb) frame_resized = cv2.resize(frame_rgb, (self.width, self.height), interpolation=cv2.INTER_LINEAR) #cv2.imshow("frame_resized", frame_resized) img_for_detect = darknet.make_image(self.width, self.height, 3) darknet.copy_image_from_bytes(img_for_detect, frame_resized.tobytes()) #print(img_for_detect.__dict__) detections = darknet.detect_image(self.network, self.class_names, img_for_detect, thresh=self.thresh) #print(detections) image = darknet.draw_boxes(detections, frame_resized, self.class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) end = time.clock() print(end - begin) cv2.imshow('Inference', image) cv2.imshow("Image Window", frame) cv2.waitKey(3)
def drawing(self, imgQue, detQue, fpsQue, msgQue): random.seed(3) # deterministic bbox colors self.window = cv2.namedWindow("Inference", cv2.WINDOW_NORMAL) cv2.moveWindow("Inference", 200, 2000) cv2.resizeWindow("Inference", self.height, self.width) if self.SAVEVID: fourcc = cv2.VideoWriter_fourcc(*"XVID") vid = cv2.VideoWriter('output.avi', fourcc, 20.0, (self.height, self.width)) while self.RUNNING: frame = imgQue.get() dets = detQue.get() fps = fpsQue.get() if frame is not None: #print(dets) image = darknet.draw_boxes(dets, frame, self.classColors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize(image, (self.height, self.width)) #if self.args.display: if self.SAVEVID: vid.write(image) cv2.imshow('Inference', image) if cv2.waitKey(fps) == 27: print("shutting down") msgQue.put(False) #vid.release() self.shutdown() vid.release() cv2.destroyAllWindows() print("Drawing completely done")
def drawing(frame_queue, detections_queue, fps_queue): """ drawing bbox on the image and writing results video file or show video image. """ # so we could release it if a signal is given global video # deterministic bbox colors random.seed(3) # results video file filename = args.out_filename # each time will open a new out file if args.out_filename: filename_split = args.out_filename.rsplit(".", 1) index = 0 while 1: # save file: name_<index>.mp4 filename = filename_split[0] + '_' + \ str(index) + '.' + filename_split[1] # file not exists if not os.path.isfile(filename): break # trying next index index += 1 # result video obj video = set_saved_video( cap, filename, (args.capture_frame_width, args.capture_frame_height)) while cap.isOpened(): frame_resized = frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() if frame_resized is not None: # draw detection bounding boxs on image image = darknet.draw_boxes(detections, frame_resized, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize( image, (args.capture_frame_width, args.capture_frame_height), interpolation=cv2.INTER_LINEAR) # writing video image if args.out_filename is not None: video.write(image) # show video image if not args.dont_show: cv2.imshow('Inference', image) # Esc key to stop GUI if cv2.waitKey(fps) == 27: break # Closes video file or capturing device cap.release() # Closes video write file video.release() # destroys all of the opened HighGUI windows cv2.destroyAllWindows() if args.out_filename: print("\nOut file: {}".format(filename))
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 pipeline(img): # image data transform # img - cv image # im - yolo image im, image = dn.array_to_image(img) dn.rgbgr_image(im) tic = time.time() result = detect2(net, meta, im) toc = time.time() print(toc - tic, result) img_final = dn.draw_boxes(img, result) return img_final
def clickAndRemove(event, x, y, flags, param): global detections, img, class_colors, orig_det image = img.copy() #clicking on a detection with the left button means remove it because it's wrong if event == cv2.EVENT_LBUTTONDOWN: new_det = [] for d in detections: if x >= d[2][0] - d[2][2] / 2. and x <= d[2][ 0] + d[2][2] / 2. and y >= d[2][ 1] - d[2][3] / 2. and y <= d[2][1] + d[2][3] / 2.: print(x, y) else: new_det.append(d) detections = new_det image = darknet.draw_boxes(detections, image, class_colors) cv2.imshow('image', image) #a click with the right button will restore the original detections of the net (in case of user mistake) if event == cv2.EVENT_RBUTTONDOWN: detections = copy.deepcopy(orig_det) image = darknet.draw_boxes(detections, image, class_colors) cv2.imshow('image', image)
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 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 detect_image(image=cv2.imread('data/dog.jpg')): darknet_image = darknet.make_image(resize_width, resize_height, 3) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (resize_width, resize_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) image = image_resized return cv2.cvtColor(image, cv2.COLOR_RGB2BGR), detections
def drawing(self): random.seed(3) # deterministic bbox colors #video = set_saved_video(cap, args.out_filename, (width, height)) while self.cap.isOpened(): frame_resized = self.frame_queue.get() detections = self.detections_queue.get() fps = self.fps_queue.get() if frame_resized is not None: image = darknet.draw_boxes(detections, frame_resized, self.class_colors) #image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #if args.out_filename is not None: #video.write(image) #if not args.dont_show: #cv2.imshow('Inference', image) self.YOLO_image_queue.put(image) #把RGB圖片存起來 if cv2.waitKey(fps) == 27: break self.cap.release()
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 drawing(frame_queue, detections_queue, fps_queue): random.seed(3) # deterministic bbox colors video = set_saved_video(cap, args.out_filename, (width, height)) while cap.isOpened(): frame_resized = frame_queue.get() detections = detections_queue.get() fps = fps_queue.get() if frame_resized is not None: image = darknet.draw_boxes(detections, frame_resized, class_colors) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if args.out_filename is not None: video.write(image) if not args.dont_show: cv2.imshow('Inference', image) if cv2.waitKey(fps) == 27: break cap.release() video.release() cv2.destroyAllWindows()
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(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) # Setup BCM Mode GPIO.setmode(GPIO.BCM) # Setup pin number pinGreen = 18 #Green led pinRed = 24 #Red led # Setup the pin as output direction valueLow = GPIO.LOW valueHigh = GPIO.HIGH GPIO.setup(pinGreen, GPIO.OUT) GPIO.setup(pinRed, GPIO.OUT) if (detections[0][0] == "with_mask"): GPIO.output(pinGreen, valueHigh) GPIO.output(pinRed, valueLow) else: GPIO.output(pinRed, valueHigh) GPIO.output(pinGreen, valueLow) # Clean it up GPIO.cleanup() 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, network, class_names, class_colors, thresh): width = darknet.network_width(network) self.__logger.info('Network width calculated') height = darknet.network_height(network) self.__logger.info('Network hight calculated') darknet_image = darknet.make_image(width, height, 3) self.__logger.info('Darknet image produced') image_rgb = image image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) self.__logger.info('Image resized') darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) self.__logger.info('Image copyed') detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh) self.__logger.info('Detection completed') darknet.free_image(darknet_image) self.__logger.info('Image freed') image = darknet.draw_boxes(detections, image_resized, class_colors) self.__logger.info('Boxes drawn') return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
def _image_detection(self, image, thresh): # 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) logger.debug('Darknet network width=%s height=%s', width, height) darknet_image = darknet.make_image(width, height, 3) logger.debug('Generating RGB resized image for Darknet') image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR) logger.debug('Copying image to darknet') darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes()) logger.debug('Running darknet.detect_image()') with suppress_stdout_stderr(suppress=not self.debug): detections = darknet.detect_image( self._network, self._names, darknet_image, thresh=thresh ) darknet.free_image(darknet_image) logger.info('Darknet result: %s', detections) image = darknet.draw_boxes(detections, image_resized, self._colors) return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections