''' predict.py有几个注意点 1、无法进行批量预测,如果想要批量预测,可以利用os.listdir()遍历文件夹,利用Image.open打开图片文件进行预测。 2、如果想要保存,利用r_image.save("img.jpg")即可保存。 3、如果想要获得框的坐标,可以进入detect_image函数,读取top,left,bottom,right这四个值。 4、如果想要截取下目标,可以利用获取到的top,left,bottom,right这四个值在原图上利用矩阵的方式进行截取。 ''' from frcnn import FRCNN from PIL import Image frcnn = FRCNN() while True: img = input('Input image filename:') try: image = Image.open(img) #-------------------------------------# # 转换成RGB图片,可以用于灰度图预测。 #-------------------------------------# image = image.convert("RGB") except: print('Open Error! Try again!') continue else: r_image = frcnn.detect_image(image) r_image.show()
frcnn = FRCNN() # 调用摄像头 capture = cv2.VideoCapture(0) # capture=cv2.VideoCapture("1.mp4") fps = 0.0 while (True): t1 = time.time() # 读取某一帧 ref, frame = capture.read() # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转变成Image frame = Image.fromarray(np.uint8(frame)) # 进行检测 frame = np.array(frcnn.detect_image(frame)) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) fps = (fps + (1. / (time.time() - t1))) / 2 print("fps= %.2f" % (fps)) frame = cv2.putText(frame, "fps= %.2f" % (fps), (0, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow("video", frame) c = cv2.waitKey(30) & 0xff if c == 27: capture.release() break
# img = 'dataset/test/images/006486001008661.jpg' # image = Image.open(img) # r_image = frcnn.detect_image(image) # r_image.save('result.jpg') testdata_file = 'dataset/main/test.txt' f = open(testdata_file) lines = f.readlines() result = open('result.txt', 'w') for l in lines: l = l.strip() img = "dataset/test/images/%s.jpg" % l image = Image.open(img) bbox, conf, label = frcnn.detect_image(image) if len(bbox) == 0: continue for i in range(len(bbox)): score = conf[i] # 置信度 left, top, right, bottom = bbox[i] # 框框位置 result.write(l) result.write(' ' + str(score)) result.write(' ' + str(left)) result.write(' ' + str(top)) result.write(' ' + str(right)) result.write(' ' + str(bottom) + '\n') result.close()
63.25, 65.25, 73.25, 60.25, 62.25, 62.25, 62.25, 62.25, 53.25, 70.25, 70.25, 71.25, 60.25, 69.25, 54.25, 74.25, 67.25, 76.25, 58.25, 63.25, 61.25, 69.25, 64.25, 70.25, 57.25, 76.25, 66.25, 66.25, 76.25, 59.25, 83.25, 63.25, 56.25, 73.25, 65.25, 43.25, 56.25, 52.25, 57.25, 80.25, 59.25, 56.25, 46.25, 53.25, 54.25, 55.25, 50.25, 58.25, 52.25, 63.25, 71.25, 52.25, 56.25, 47.25, 49.25, 60.25, 71.25, 54.25, 51.25, 63.25, 53.25, 51.25, 39.25, 45.25, 49.25, 51.25, 38.25, 51.25, 50.25, 63.25, 35.25, 44.25, 60.25, 91.25, 47.25, 43.25, 49.25, 54.25, 33.25, 48.25, 61.25, 52.25, 49.25, 38.25, 56.25, 40.25, 41.25, 25.25, 43.25, 39.25, 34.25, 59.25, 57.25, 32.25, 48.25, 40.25, 52.25, 22.25, 39.25, 47.25, 53.25, 38.25, 36.25, 32.25, 58.25, 45.25, 49.25, 45.25, 47.25, 33.25, 65.25, 31.25, 45.25, 25.25, 36.25, 47.25, 21.25, 53.25, 41.25, 37.25, 51.25, 58.25, 43.25, 36.25, 44.25, 27.25, 46.25, 45.25, 29.25, 41.25, 41.25, 57.25, 44.25, 49.25, 34.25, 47.25, 38.25, 69.25, 32.25, 51.25, 45.25, 21.25, 36.25, 41.25, 51.25, 55.25, 30.25, 45.25, 30.25, 27.25, 42.25, 46.25, 54.25, 32.25, 29.25, 32.25, 46.25, 41.25, 13.25, 18.25, 42.25, 25.25, 47.25, 33.25, 47.25, 33.25, 20.25, 38.25, 30.25, 34.25, 41.25, 32.25, 26.25, 48.25, 44.25, 39.25, 56.25, 58.25, 32.25, 25.25, 32.25, 43.25, 40.25, 31.25, 35.25, 44.25, 42.25, 39.25, 43.25, 43.25, 97.25, 29.25, 29.25, 54.25, 27.25, 32.25, 39.25, 24.25, 41.25, 21.25, 16.25, 28.25, 52.25, 38.25, 26.25, 28.25, 31.25, 34.25, 42.25, 57.25, 25.25, 9.25, 42.25, 40.25, 30.25, 30.25, 49.25, 37.25, 51.25, 39.25, 54.25, 35.25, 58.25, 33.25, 46.25, 41.25, 47.25, 34.25, 36.25, 42.25, 41.25, 23.25, 66.25, 25.25, 34.25, 32.25, 37.25, 18.25, 37.25, 35.25, 56.25, 45.25, 47.25, 38.25, 47.25, 43.25, 40.25, 47.25, 58.25, 32.25, 40.25, 10.25, 7.25, 8.25, -5.75, 47.25, 44.25, 42.25, 49.25, 51.25, 57.25, 39.25, 26.25, 26.25, 29.25, 39.25, 39.25, -19.75, 0.25, 4.25, -4.75 ] alter_raman = frcnn.detect_image(raman) frcnn.close_session()
2、如果想要进行检测完的图片的保存,利用r_image.save("img.jpg")即可保存,直接在predict.py里进行修改即可。 3、如果想要获得预测框的坐标,可以进入frcnn.detect_image函数,在绘图部分读取top,left,bottom,right这四个值。 4、如果想要利用预测框截取下目标,可以进入frcnn.detect_image函数,在绘图部分利用获取到的top,left,bottom,right这四个值 在原图上利用矩阵的方式进行截取。 5、如果想要在预测图上写额外的字,比如检测到的特定目标的数量,可以进入frcnn.detect_image函数,在绘图部分对predicted_class进行判断, 比如判断if predicted_class == 'car': 即可判断当前目标是否为车,然后记录数量即可。利用draw.text即可写字。 ''' while True: img = input('Input image filename:') try: image = Image.open(img) except: print('Open Error! Try again!') continue else: r_image = frcnn.detect_image(image, crop=crop) r_image.show() elif mode == "video": capture = cv2.VideoCapture(video_path) if video_save_path != "": fourcc = cv2.VideoWriter_fourcc(*'XVID') size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter(video_save_path, fourcc, video_fps, size) fps = 0.0 while (True): t1 = time.time() # 读取某一帧 ref, frame = capture.read()
help='num img 2 show', default=1) parser.add_argument('-r', "--root", type=str, help='root dir filled with *.jpg') parser.add_argument('-i', "--filename", type=str, help='filename', default='') args = parser.parse_args() frcnn = FRCNN(args.model_path, args.backbone, args.conf) # efficientdet = EfficientDet(args.model_path, args.version, args.conf, args.cuda) if args.num2show == 1: image = Image.open(os.path.join(args.root, args.filename)) res, cls, score = frcnn.detect_image(image) print(cls, score) else: print('结果将会保存到temp.png') files = os.listdir(args.root) idx = [ int(len(os.listdir(args.root)) * random.random()) for i in range(args.num2show) ] imgs = [Image.open(os.path.join(args.root, files[id])) for id in idx] ress, clss, scores = [], [], [] print(len(imgs)) for img in imgs: res, cls, score = frcnn.detect_image(img) ress.append(res)
2、如果想要进行检测完的图片的保存,利用r_image.save("img.jpg")即可保存,直接在predict.py里进行修改即可。 3、如果想要获得预测框的坐标,可以进入frcnn.detect_image函数,在绘图部分读取top,left,bottom,right这四个值。 4、如果想要利用预测框截取下目标,可以进入frcnn.detect_image函数,在绘图部分利用获取到的top,left,bottom,right这四个值 在原图上利用矩阵的方式进行截取。 5、如果想要在预测图上写额外的字,比如检测到的特定目标的数量,可以进入frcnn.detect_image函数,在绘图部分对predicted_class进行判断, 比如判断if predicted_class == 'car': 即可判断当前目标是否为车,然后记录数量即可。利用draw.text即可写字。 ''' while True: img = input('Input image filename:') try: image = Image.open(img) except: print('Open Error! Try again!') continue else: r_image = frcnn.detect_image(image) r_image.show() elif mode == "video": capture = cv2.VideoCapture(video_path) if video_save_path != "": fourcc = cv2.VideoWriter_fourcc(*'XVID') size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter(video_save_path, fourcc, video_fps, size) fps = 0.0 while (True): t1 = time.time() # 读取某一帧 ref, frame = capture.read()