def detect_img(indir,outdir): ssd = SSD() #遍历该目录下的所有图片文件 for filename in glob.glob(indir): print("Start, the detect image is:",filename) img = Image.open(filename) img = ssd.detect_image(img) #img.show() # 显示图片 img.save(os.path.join(outdir,os.path.basename(filename))) print("End, the detection of this image") print('---------------------------------')
#-------------------------------------# from keras.layers import Input from ssd import SSD from PIL import Image import numpy as np import cv2 ssd = SSD() # 调用摄像头 capture = cv2.VideoCapture(0) # capture=cv2.VideoCapture("1.mp4") while (True): # 读取某一帧 ref, frame = capture.read() # 格式转变,BGRtoRGB frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转变成Image frame = Image.fromarray(np.uint8(frame)) # 进行检测 frame = np.array(ssd.detect_image(frame)) # RGBtoBGR满足opencv显示格式 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) cv2.imshow("video", frame) c = cv2.waitKey(30) & 0xff if c == 27: capture.release() break yolo.close_session()
1、如果想要进行检测完的图片的保存,利用r_image.save("img.jpg")即可保存,直接在predict.py里进行修改即可。 2、如果想要获得预测框的坐标,可以进入ssd.detect_image函数,在绘图部分读取top,left,bottom,right这四个值。 3、如果想要利用预测框截取下目标,可以进入ssd.detect_image函数,在绘图部分利用获取到的top,left,bottom,right这四个值 在原图上利用矩阵的方式进行截取。 4、如果想要在预测图上写额外的字,比如检测到的特定目标的数量,可以进入ssd.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 = ssd.detect_image(image, crop = crop, count=count) 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) ref, frame = capture.read() if not ref: raise ValueError("未能正确读取摄像头(视频),请注意是否正确安装摄像头(是否正确填写视频路径)。") fps = 0.0 while(True):
from keras.layers import Input from ssd import SSD from PIL import Image ssd = SSD() while True: img = input('Input image filename:') try: image = Image.open(img) except: print('Open Error! Try again!') continue else: r_image = ssd.detect_image(image) r_image.show()
2、如果想要进行检测完的图片的保存,利用r_image.save("img.jpg")即可保 存,直接在predict.py里进行修改即可。 3、如果想要获得预测框的坐标,可以进入ssd.detect_image函数,在绘图部分读取top,left,bottom,right这四个值。 4、如果想要利用预测框截取下目标,可以进入ssd.detect_image函数,在绘图部分利用获取到的top,left,bottom,right这四个值 在原图上利用矩阵的方式进行截取。 5、如果想要在预测图上写额外的字,比如检测到的特定目标的数量,可以进入ssd.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 = ssd.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()
help='root dir filled with *.jpg', default='VOCdevkit/VOC2007/JPEGImages') parser.add_argument('-i', "--filename", type=str, help='filename', default='') args = parser.parse_args() model = SSD(args.model_path, args.conf, args.cuda) if args.num2show == 1: image = Image.open(os.path.join(args.root, args.filename)) res, cls, score = efficientdet.detect_image(image) print(cls, score) # r_image.show() 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 = model.detect_image(img) ress.append(res) clss.append(cls) scores.append(score) show_imgs(ress, clss, scores)