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 run(self): print("\nDetectImage线程执行\n") import darknet config_file = self.projectPath + "yolo-obj.cfg" data_file = self.projectPath + "obj.data" weights = self.projectPath + 'backup/' + self.weight print(config_file) print(data_file) print(weights) print(self.imageName) print("\nnet装载\n") self.DetectImageSignal.emit(0, [0], {"0": 0}) network, class_names, class_colors = darknet.load_network( config_file, data_file, weights) # 获取到net self.DetectImageSignal.emit(1, [0], {"0": 0}) image = darknet.load_image(self.imageName.encode("utf-8"), 0, 0) self.DetectImageSignal.emit(2, [0], {"0": 0}) detections = darknet.detect_image(network, class_names, image) # del darknet self.DetectImageSignal.emit(3, detections, class_colors) print("\nDetectImage线程执行完毕\n")
def detect(filename, threshold): im = darknet.load_image(bytes(filename, "ascii"), 0, 0) r = darknet.detect_image(network, class_names, im, thresh=threshold) darknet.free_image(im) # Convert confidence from string to float: if len(r) > 0: for i in range(len(r)): r[i] = (r[i][0], float(r[i][1]), r[i][2]) return r
def detect(): print('Loading image') # Use tmp.jpg for demo purposes im = dn.load_image(bytes('tmp.jpg', encoding='utf-8'), 0, 0) num = dn.c_int(0) pnum = dn.pointer(num) print('Predicting image') dn.predict_image(net, im) print('Getting boxes') dets = dn.get_network_boxes(net, im.w, im.h, 0.5, 0.5, None, 1, pnum) print('Marking boxes') res = [] classes = 1 for j in range(num.value): for i in range(classes): if dets[j].prob[i] > 0.75: b = dets[j].bbox res.append((b.x, b.y, b.w, b.h)) dn.free_image(im) dn.free_detections(dets, num) print('Saving image') source_img = Image.open('tmp.jpg').convert("RGB") size = source_img.size w = size[0] h = size[1] draw = ImageDraw.Draw(source_img) for b in res: x1 = (b[0] - b[2] / 2.) * w x2 = (b[0] + b[2] / 2.) * w y1 = (b[1] - b[3] / 2.) * h y2 = (b[1] + b[3] / 2.) * h draw.rectangle(((x1, y1), (x2, y2)), outline="red") print(b) source_img.save('tmp.jpg', "JPEG")
def detect_from_file(net, meta, image_path, thresh=.5, hier_thresh=.5, nms=.45, debug=False): #pylint: disable= C0321 if USING_DARKNET_IMAGE_IO: im = darknet.load_image(to_str(image_path, True), 0, 0) else: import cv2 custom_image = cv2.imread(to_str(image_path)) im, arr = darknet.array_to_image(custom_image) if debug: print("Loaded image") det = detect_from_memory(net, meta, im, thresh, hier_thresh, nms, debug) if USING_DARKNET_IMAGE_IO: darknet.free_image(im) if debug: print("freed image") return det
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): """ From darknet/python/darknet.py """ # im = dn.load_image(image, 0, 0) im = dn.load_image(image, dn.network_width(net), dn.network_height(net)) boxes = dn.make_boxes(net) probs = dn.make_probs(net) num = dn.num_boxes(net) dn.network_detect(net, im, thresh, hier_thresh, nms, boxes, probs) res = [] for j in range(num): for i in range(meta.classes): if probs[j][i] > 0: res.append((i, probs[j][i], (boxes[j].x, boxes[j].y, boxes[j].w, boxes[j].h))) res = sorted(res, key=lambda x: -x[1]) dn.free_image(im) dn.free_ptrs(dn.cast(probs, dn.POINTER(dn.c_void_p)), num) return res
import sys import os import numpy as np import cv2 import jin_cam as cam import motorcontrol as motor import darknet as dn #for yolo variable net = dn.load_net( "/home/pi/darknet-nnpack/python/wild_boar_tiny_yolo/jin_yolov3-tiny.cfg", "/home/pi/darknet-nnpack/python/wild_boar_tiny_yolo/jin_yolov3-tiny_final.weights", 0) meta = dn.load_meta("/home/pi/darknet-nnpack/python/obj.data") im = dn.load_image("/home/pi/darknet-nnpack/python/1.jpeg", 0, 0) #camera setting variable CV_CAP_PROP_FRAME_WIDTH = 480 CV_CAP_PROP_FRAME_HEIGHT = 320 CV_CAP_PROP_FPS = 5 #calibration parameter # K = [fx, skew*fx, cx], [0, fy, cy], [0, 0, 1] # Distortion Coefficients(kc) - 1st, 2nd l_K = np.array([[1394.68, 0., 598.24], [0, 1388.52, 389.33], [0, 0, 1]]) l_D = np.array([-0.71, 5.5, 0, 0, 0]) # just use first two terms r_K = np.array([[1525, 0., 584.76], [0, 1570.7, 724.86], [0, 0, 1]]) r_D = np.array([0.11, -0.65, 0, 0, 0]) # just use first two terms #open cam
def detect(self, frame): frame = 'tmp/' + frame im = darknet.load_image(frame, 0, 0) r = darknet.detect(self.NET, self.META, frame, thresh=self.THRESH) return r
import darknet import cv2 imagename = "data/0a3a99129a9e37253f49c2b25283d2fb.jpg" networkspp, _, _ = darknet.load_network('cfg/anprspp.cfg', 'test.data', 'weights/anprspp_final.weights') networktiny, _, _ = darknet.load_network('cfg/anprtiny.cfg', 'test.data', 'weights/anprtiny_final.weights') networkyv3, _, _ = darknet.load_network('cfg/anpryolov3.cfg', 'test.data', 'weights/anpryolov3_final.weights') networkplate, _, _ = darknet.load_network('cfg/plate.cfg', 'test.data', 'weights/plate_final.weights') img = darknet.load_image(imagename.encode("ascii"), 0, 0) rspp = darknet.detect_image(networkspp, ["plate"], img) rtiny = darknet.detect_image(networktiny, ["plate"], img) ryv3 = darknet.detect_image(networkyv3, ["plate"], img) rplate = darknet.detect_image(networkplate, ["plate"], img) print("rssp", rspp) print("rtiny", rtiny) print("ryv3", ryv3) print("rplate", rplate) img = cv2.imread(imagename) img = darknet.draw_boxes(rspp, img, {"plate": (255, 0, 0)}) img = darknet.draw_boxes(rtiny, img, {"plate": (0, 255, 0)}) img = darknet.draw_boxes(ryv3, img, {"plate": (0, 0, 255)})
if __name__ == "__main__": lightnet.set_cwd(dir) net, meta = lightnet.load_network_meta("obj.cfg", "weights/obj_200.weights", "obj.data") # "../../bin/cfg/darknet19_448.cfg", "../../bin/darknet19_448.weights", "../../bin/cfg/imagenet1k.data") if IMAGE_MODE: if True: frame = cv.imread(lightnet.to_str('test.jpg')) im, arr = darknet.array_to_image(frame) darknet.rgbgr_image(im) else: im = darknet.load_image( lightnet.to_str('test.jpg').encode("ascii"), 0, 0) r = darknet.classify(net, meta, im) print(r) else: cap = cv.VideoCapture(0) if not cap.isOpened(): raise Exception('Fail to open %s' % (0)) while True: hasFrame, frame = cap.read() if not hasFrame: cv.waitKey() break # cv.imwrite('test.jpg', frame) cols = frame.shape[1] rows = frame.shape[0]
return net, meta if __name__ == '__main__': import os classify_modu = load_classify_module( b"./config/new_chinese_classify.cfg", b"./classify_checkpoints/new_chinese_classify_72.weights", b"./config/chinese_classify.data") # classify_modu = load_classify_module(b"./config/alexnet.cfg", # b"./classify_checkpoints/alexnet_1129.weights", # b"./config/chinese_classify.data") VALID_PATH = './classify_data/valid' images = os.listdir(VALID_PATH) images.remove('.gitignore') truth_count = 0 for image_name in images: path = os.path.join(VALID_PATH, image_name) _, unicode_name = image_name[:-4].split('_') img = load_image(path.encode(), 0, 0) print(path) res = classify(classify_modu[0], classify_modu[1], img) flag = False if unicode_name in [pred_item[0].decode() for pred_item in res[0:5]]: flag = True truth_count += 1 print('原图为', unicode_name, '识别结果为:', res[0:5], flag) print('Truth count:', truth_count)
def crack(img_path, dtc_modu, classify_modu, k): # 定位汉字,返回多个矩形框 print('\n' * 2 + '定位汉字' + '\n' + '*' * 80) d = time.time() rets = detect(dtc_modu[0], dtc_modu[1], img_path.encode()) print('定位汉字耗时{}'.format(time.time() - d)) l = len(rets) # 设置阈值 if l > k: return 0 # 切割图片,返回切割后的汉字图片 print('\n' * 2 + '切割图片' + '\n' + '*' * 80) s = time.time() hanzi_list = seg_one_img(img_path, rets) # print(hanzi_list)mmmmmmmmmmmmmm print('切割图片耗时{}'.format(time.time() - s)) # 汉字识别,返回汉字字符串 print('\n' * 2 + '汉字识别' + '\n' + '*' * 80) r = time.time() all_hanzi_lists = [] # 存储所有汉字的列表 # 提取路径存入列表 paths = [] for per in hanzi_list: paths.extend([i for i in per.keys()]) for path in paths: # 对切割的汉字图片进行遍历 hanzis = [] img = load_image(path.encode(), 0, 0) res = classify(classify_modu[0], classify_modu[1], img) print(res[0:5]) if res[0][1] < 0.95: for hz in res[0:5]: # 对识别的top5进行遍历 hanzi = ('\\' + hz[0].decode('utf-8') ).encode('utf-8').decode('unicode_escape') hanzis.append(hanzi) else: hanzi = ('\\' + res[0][0].decode('utf-8') ).encode('utf-8').decode('unicode_escape') hanzis.append(hanzi) all_hanzi_lists.append(hanzis) # print(all_hanzi_lists)mmmmmmmmmmmmmmmmmmmmmmmmmm hanzi_combination = combination(*all_hanzi_lists) # print(hanzi_combination) hanzi_combination_connect = [] for words in hanzi_combination: hanzi_combination_connect.append(''.join(words)) # print(hanzi_combination_connect)mmmmmmmmmmmmmmmmmmmmm print('汉字识别耗时{}'.format(time.time() - r)) # 识别语序 hanzi_center = [] jieba_flag = 0 o = time.time() print('\n' * 2 + '语序识别' + '\n' + '*' * 80) for words in hanzi_combination_connect: # 对每一个组合进行结巴分词 # 此处对汉字的坐标进行记忆 hanzi_center = recordCoordinate(words, hanzi_list) # print(hanzi_center, 'jiaba')mmmmmmmmmmmmm o = time.time() rec_word_possible = recog_order_jieba(words) if rec_word_possible: # 如果遇到正确的词,则标志位置1 jieba_flag = 1 break if jieba_flag: rec_word = rec_word_possible else: hanzi_center = recordCoordinate(hanzi_combination_connect[0], hanzi_list) # print(hanzi_center, 'engine')mmmmmmmmmmmmmmm rec_word = search_engine_recog(hanzi_combination_connect[0]) print('语序识别结果:{}'.format(rec_word)) print('语序识别耗时{}'.format(time.time() - o)) # 按正确语序输出坐标 print('\n' * 2 + '最终结果' + '\n' + '*' * 80) centers = [] for i in rec_word: centers.append(hanzi_center[i]) print('正确语序的坐标:{}'.format(centers)) print('总耗时{}'.format(time.time() - d)) ## 调用时需要返回坐标 return (rec_word)
def main(): # --------------- Configurable parameters start ----------- img_extn = '.png' label_extn = '.txt' img_list_path = '/home/govind/work/projects/ivs1+_dir/darknet_dir/darknet/external/ivs_val.txt' img_label_dir = '/home/govind/work/projects/ivs1+_dir/darknet_dir/data/val/labels' # We would create a new directory where 3 things: labeled images, transformed gt, detected labels will be dumped output_dir = '/home/govind/work/projects/ivs1+_dir/darknet_dir/darknet/external/output' # --------------- Configurable parameters end ----------- # Sanity checks assert os.path.exists(img_list_path) assert os.path.exists(img_label_dir) # Get a list of image paths with open(img_list_path, 'r') as fp: img_list = [ x.rstrip('\n') for x in fp.readlines() if x.endswith(img_extn + '\n') ] assert len(img_list) != 0 # Sanity check # Create necessary directories os.makedirs(output_dir) out_gt_dir = os.path.join(output_dir, 'gt_labels') out_det_dir = os.path.join(output_dir, 'det_labels') out_img_dir = os.path.join(output_dir, 'labeled_images') os.makedirs(out_gt_dir, exist_ok=True) os.makedirs(out_det_dir, exist_ok=True) os.makedirs(out_img_dir, exist_ok=True) # Loop over all image samples for i, img_path in enumerate(img_list): print('\rProcessing image {:d} of {:d}'.format(i, len(img_list)), end='') assert os.path.exists(img_path) # Image must exist img_name = os.path.basename(img_path) label_file = img_name.replace(img_extn, label_extn) label_file_path = os.path.join(img_label_dir, label_file) assert os.path.exists(label_file_path) # Check if gt label exists img = cv2.imread(img_path) # Read image assert img is not None # Verify that image-read was successful img_ht, img_wd, _ = img.shape # --------- Create gt label file --------- # Read gt for this image sample with open(label_file_path, 'r') as fp: gt_lines = fp.readlines() gt_boxes = [] with open(os.path.join(out_gt_dir, label_file), 'w') as fp: # Create a new label file out_str = '' for line in gt_lines: # Loop over all boxes # We should bring the bboxes back to their absolute scale entries = line.rstrip('\n').split(' ') entries[1], entries[3] = str(int( float(entries[1]) * img_wd)), str( int(float(entries[3]) * img_wd)) entries[2], entries[4] = str(int( float(entries[2]) * img_ht)), str( int(float(entries[4]) * img_ht)) out_str += ' '.join(entries) + '\n' gt_boxes.append({'box': [int(x) for x in entries[1:5]]}) fp.write(out_str) # Write all boxes to output label file # --------- Create detected label file --------- boxes = [] # Would hold bounding boxes det_result = dn.detect(net, meta, dn.load_image(img_path.encode('ascii'), 0, 0)) # Perform object detection for box in det_result: # Loop over all detected bounding boxes boxes.append({ 'conf': box[1], 'box': box[2] }) # Coordinates are x_mid, y_mid, wd, ht with open(os.path.join(out_det_dir, label_file), 'w') as fp: # Create a new label file out_str = '' for person in boxes: # Loop over all boxes box_str = [str(int(x)) for x in person['box']] out_str += ' '.join(['0'] + [str(person['conf'])] + box_str + ['\n']) # '0' for 'person' class ID fp.write(out_str) # Write all boxes to output label file # --------- Dump labeled image --------- draw_bboxes(img, gt_boxes, color=(0, 255, 0)) # Draw gt bboxes draw_bboxes(img, boxes, color=(0, 0, 255)) # Draw detected bboxes cv2.imwrite(os.path.join(out_img_dir, img_name), img) print("Done") return
assert os.path.exists(img_path) img = cv2.imread(img_path) assert img is not None img_ht, img_wd, _ = img.shape for person in boxes: box = person['box'] xtl = int(max(0, box[0] - box[2] / 2)) ytl = int(max(0, box[1] - box[3] / 2)) xbr = int(min(img_wd - 1, box[0] + box[2] / 2)) ybr = int(min(img_ht - 1, box[1] + box[3] / 2)) cv2.rectangle(img, (xtl, ytl), (xbr, ybr), color=(0, 0, 255), thickness=2) out_img_path = os.path.join(results_dir, os.path.basename(img_path)) cv2.imwrite(out_img_path, img) print('Dumped labeled image: {:s}'.format(out_img_path)) for image_path in images: boxes = [] det_result = dn.detect(net, meta, dn.load_image(image_path, 0, 0)) for box in det_result: # Loop over all detected bounding boxes boxes.append({'conf': box[1], 'box': box[2]}) print(boxes) draw_bboxes(image_path, boxes)
parser.add_argument('--camera', type=int, default=0) parser.add_argument('--top_k', type=int, default=3) parser.add_argument('--gold_confidence', type=float, default=0.95) parser.add_argument('--display_confidence', type=float, default=0.5) args = parser.parse_args() net, meta = lightnet.load_network_meta(args.config, args.weights, args.data) if args.image is not None: if True: frame = cv.imread(lightnet.to_str('test.jpg')) im, arr = darknet.array_to_image(frame) darknet.rgbgr_image(im) else: im = darknet.load_image(lightnet.to_str('test.jpg', True), 0, 0) r = darknet.classify(net, meta, im) print(r) else: cap = cv.VideoCapture(args.camera) if not cap.isOpened(): raise Exception('Fail to open %s' % (0)) while True: hasFrame, frame = cap.read() if not hasFrame: cv.waitKey() break im, arr = darknet.array_to_image(frame) darknet.rgbgr_image(im)