def detect(self, image: str, thresh: float = .5, hier_thresh: float = .5, nms: float = .45) -> list: if isinstance(image, str): im = dn.load_image(image.encode(), 0, 0) elif image is None: return [] else: arr = image.transpose(2, 0, 1) c, h, w = arr.shape arr = (arr/255.0).flatten() data = dn.c_array(dn.c_float, arr) im = dn.IMAGE(w, h, c, data) num = dn.c_int(0) pnum = dn.pointer(num) dn.predict_image(self.net, im) dets = dn.get_network_boxes( self.net, im.w, im.h, thresh, hier_thresh, None, 0, pnum) num = pnum[0] if (nms): dn.do_nms_obj(dets, num, self.meta.classes, nms) res = [] for j in range(num): if dets[j].prob[PERSON_ID] > 0: bb = dets[j].bbox res.append((dets[j].prob[PERSON_ID], BBOX(bb))) res = sorted(res, key=lambda x: -x[0]) # 0 is prob # dn.free_image(im) # raise double free error dn.free_detections(dets, num) return res
def array_to_image(arr): # need to return old values to avoid python freeing memory arr = arr.transpose(2, 0, 1) c, h, w = arr.shape[0:3] arr = np.ascontiguousarray(arr.flat, dtype=np.float32) / 255.0 data = arr.ctypes.data_as(dn.POINTER(dn.c_float)) im = dn.IMAGE(w, h, c, data) return im, arr
def array_to_image(arr): arr = arr.transpose(2, 0, 1) c = arr.shape[0] h = arr.shape[1] w = arr.shape[2] arr = (arr / 255.0).flatten() data = dn.c_array(dn.c_float, arr) im = dn.IMAGE(w, h, c, data) return im
def array_to_image(array): array = array.transpose(2, 0, 1) c = array.shape[0] h = array.shape[1] w = array.shape[2] array = (array / 255.0).flatten() data = darknet.c_array(darknet.c_float, array) image = darknet.IMAGE(w, h, c, data) return image
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 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 array_to_image(arr): time1 = time.time() arr = arr.transpose(2, 0, 1) time2 = time.time() print("transpose took: ", time2 - time1) c = arr.shape[0] h = arr.shape[1] w = arr.shape[2] arr = (arr / 255.0).flatten() time3 = time.time() print("flattern took: ", time3 - time2) data = dn.c_array(dn.c_float, arr) time4 = time.time() print("change to c_array took: ", time4 - time3) im = dn.IMAGE(w, h, c, data) time5 = time.time() print("final loading IMAGE from array took: ", time5 - time4) return im
def detect(self, img, threshold=0.24): if self.__net is not None: net_w = self.__net_size[0] net_h = self.__net_size[1] # レターボックス形式に変換する letter_box, st_x, st_y = YOLO.get_letter_box(img, net_w, net_h) # ctypesで受け渡せる形式に変換する darknet_img_data = YOLO.get_darknet_img_data(letter_box) args_data = darknet_img_data.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) img_dn_lb = dn.IMAGE(w = net_w, h = net_w, c = 3, data = args_data) # 物体検出を実行する result = dn.detect(self.__net, self.__meta, img_dn_lb, threshold) org_size = (img.shape[1], img.shape[0]) lb_size = (letter_box.shape[1], letter_box.shape[0]) # レターボックスの座標系からオリジナルの座標系に変換する result = YOLO.get_original_coords_result(result, org_size, lb_size, st_x, st_y) return result else: raise Exception("execute detection exception: network is None")
batch_size=batch_size) os.remove(tmp_config_path) stdin = sys.stdin.detach() while True: buf = stdin.read(batch_size * width * height * 3) if not buf: break arr = numpy.frombuffer(buf, dtype='uint8').reshape( (batch_size, height, width, 3)) arr = arr.transpose((0, 3, 1, 2)) arr = numpy.ascontiguousarray(arr.flat, dtype='float32') / 255.0 darknet_images = arr.ctypes.data_as(darknet.POINTER(darknet.c_float)) darknet_images = darknet.IMAGE(width, height, 3, darknet_images) raw_detections = darknet.network_predict_batch(net, darknet_images, batch_size, width, height, threshold, 0.5, None, 0, 0) detections = [] for idx in range(batch_size): num = raw_detections[idx].num raw_dlist = raw_detections[idx].dets darknet.do_nms_obj(raw_dlist, num, len(class_names), 0.45) raw_dlist = darknet.remove_negatives(raw_dlist, class_names, num) dlist = [] for cls, score, (cx, cy, w, h) in raw_dlist: dlist.append({ 'Category': cls, 'Score': float(score), 'Left': int(cx - w / 2),
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 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())
def darknet_batch_detection_rs_images(network, image_path, save_dir, patch_groups, patch_count, class_names, batch_size, thresh=0.25, hier_thresh=.5, nms=.45): ''' # run batch detection of YOLO on an remote sensing image. :param network: a darknet network (already load the weight) :param image_path: the image patch. :param patch_groups: the group of images patch, each group has the same width and height. :param class_names: :param batch_size: :param thresh: :param hier_thresh: :param nms: :return: ''' # read the entire image entire_img_data, nodata = raster_io.read_raster_all_bands_np(image_path) entire_img_data = entire_img_data.transpose(1, 2, 0) # to opencv format # # RGB to BGR: Matplotlib image to OpenCV https://www.scivision.dev/numpy-image-bgr-to-rgb/ # entire_img_data = entire_img_data[..., ::-1].copy() # cancel RGB to BGR, since it make results worse, (maybe darknet already read images as RGB during training) entire_height, entire_width, band_num = entire_img_data.shape print("entire_height, entire_width, band_num", entire_height, entire_width, band_num) if band_num not in [1, 3]: raise ValueError('only accept one band or three band images') # class_colors={} #{'thaw_slump': (139, 140, 228)} # class_colors should get when load network # for name in class_names: # class_colors[name] = (0,0,0) patch_idx = 0 for key in patch_groups.keys(): patches_sameSize = patch_groups[key] batch_patches = [ patches_sameSize[i * batch_size:(i + 1) * batch_size] for i in range((len(patches_sameSize) + batch_size - 1) // batch_size) ] for a_batch_patch in batch_patches: t0 = time.time() input_batch_size = len(a_batch_patch) if input_batch_size != batch_size: if input_batch_size > batch_size: raise ValueError( 'input_batch_size (%d) is greater than batch_size (%d)' % (input_batch_size, batch_size)) while (len(a_batch_patch) < batch_size): a_batch_patch.append( a_batch_patch[0] ) # copy the first one to fit the batch size images = [ copy_one_patch_image_data(patch, entire_img_data) for patch in a_batch_patch ] # for the batch prediction, it seems have to resize to the network size img_height, img_width, band_num = images[0].shape width = darknet.network_width(network) height = darknet.network_height(network) # darknet_images = prepare_batch(images, network) # prepare_batch darknet_images = [] for image in images: # print('image shape',image.shape) image_resized = cv2.resize(image, (width, height), interpolation=cv2.INTER_LINEAR) if band_num == 1: image_resized = np.expand_dims( image_resized, axis=2) # add one diminsion at the last # print('image_resized shape', image_resized.shape) 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)) darknet_images = darknet.IMAGE(width, height, band_num, darknet_images) # prediction batch_detections = darknet.network_predict_batch( network, darknet_images, batch_size, img_height, img_width, thresh, hier_thresh, None, 0, 0) batch_predictions = [] for idx in range(input_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] = np.ascontiguousarray(images[idx]) # images[idx] = darknet.draw_boxes(predictions, images[idx], class_colors) batch_predictions.append(predictions) darknet.free_batch_detections(batch_detections, batch_size) for idx, (patch, image, predictions) in enumerate( zip(a_batch_patch, images, batch_predictions)): # remove the duplicated ones if idx >= input_batch_size: break # save results save_res_json = os.path.join(save_dir, '%d.json' % patch_idx) # the confidence output by network_predict_batch is 0-1, convert to percent save_one_patch_detection_json(patch, predictions, class_names, save_res_json, b_percent=True) # cv2.imwrite('%d.png'%patch_idx, image) # save for testing # for label, confidence, bbox in predictions: # bbox = darknet.bbox2points(bbox) # to [xmin, ymin, xmax, ymax] # print(label, class_names.index(label), bbox, confidence) if patch_idx % 100 == 0: print('saving %d patch, total: %d, cost %f second' % (patch_idx, patch_count, (time.time() - t0) / batch_size)) patch_idx += 1
dn.set_gpu(0) net = dn.load_net("6_15_test.cfg", "6_15_test_40000.weights", 0) meta = dn.load_meta("test.data") bus = pc2.BusManager() cam = pc2.Camera() cam.connect(bus.getCameraFromIndex(0)) cam.startCapture() startTime = time.time() while True: print "Time was", time.time() - startTime startTime = time.time() image = cam.retrieveBuffer() image = image.convert(pc2.PIXEL_FORMAT.BGR) img = np.array(image.getData(), dtype="uint8").reshape( (image.getRows(), image.getCols(), 3)) yoloImage = dn.IMAGE() detections = dn.detect_np(net, meta, img) for detection in detections: loc = detection[2] cv2.rectangle( img, (int(loc[0] - (.5 * loc[2])), int(loc[1] - (.5 * loc[3]))), (int(loc[0] + (.5 * loc[2])), int(loc[1] + (.5 * loc[3]))), (0, 0, 255)) cv2.imshow("Test", img) key = cv2.waitKey(1) if key == ord("q"): break
arr = arr.flatten() toc = timeit.default_timer() flattening += (toc - tic) tic = timeit.default_timer() a = np.copy(arr).astype(np.float32) toc = timeit.default_timer() copy_and_convert_to_float32 += (toc - tic) tic = timeit.default_timer() new_arr = np.ctypeslib.as_ctypes(a) toc = timeit.default_timer() ctyping += (toc - tic) tic = timeit.default_timer() im = dn.IMAGE(w, h, c, new_arr) toc = timeit.default_timer() structuring += (toc - tic) tic = timeit.default_timer() dn.rgbgr_image(im) toc = timeit.default_timer() rgbgring += (toc - tic) tic = timeit.default_timer() big_pull_results[str(tiff).split("'")[1].split('/') [-1]] = dn.detect(net, meta, im) toc = timeit.default_timer() yoloing += toc - tic t += 1 image_toc = timeit.default_timer()