Yolov5通过图片链接来识别图片

因为要做个暑期项目,队友是通过oss来存储图片,不是本地识别,要从链接来识别。又因为打开文件是字节流,不是图片格式,所以就直接用opencv的视频功能来完成。

在detect.py文件下。
1.首先是配置,该改的配置如下,其他可以

def run(weights='runs/train/exp5/weights/best.pt',  # model.pt path(s) 你训练好的模型
        source='https://tourist-insect.oss-cn-shanghai.aliyuncs.com/tmp/2021/07/21/16268285775224793.jpg',  # 这个就是你要识别图片的url
        imgsz=640,  # inference size (pixels)
        conf_thres=0.25,  # confidence threshold
        iou_thres=0.45,  # NMS IOU threshold
        max_det=1000,  # maximum detections per image
        device='',  # cuda device, i.e. 0 or 0,1,2,3 or cpu
        view_img=False,  # show results
        save_txt=True,  # save results to *.txt
        save_conf=False,  # save confidences in --save-txt labels
        save_crop=False,  # save cropped prediction boxes
        nosave=False,  # do not save images/videos
        classes=None,  # filter by class: --class 0, or --class 0 2 3
        agnostic_nms=False,  # class-agnostic NMS
        augment=False,  # augmented inference
        visualize=False,  # visualize features
        update=False,  # update all models
        project='runs/detect',  # save results to project/name
        name='exp',  # save results to project/name
        exist_ok=False,  # existing project/name ok, do not increment
        line_thickness=3,  # bounding box thickness (pixels)
        hide_labels=False,  # hide labels
        hide_conf=False,  # hide confidences
        half=False,  # use FP16 half-precision inference
        ):

2.其次就是获取图片并保存到本地
这一步是将图片保存为temp.jpg

    cap = cv2.VideoCapture(source)
    if( cap.isOpened() ):
        ret, img = cap.read()
        img = cv2.resize(img, (640, 640))
        cv2.imwrite("temp.jpg", img)#将网络的图片存到本地

有的图片cap.isOpened()是False,那么可以用另一种方法

	resp = urllib.request.urlopen(source)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    cv2.imwrite("temp.jpg", image)

3.最后加入数据集

dataset = LoadImages('temp.jpg', img_size=imgsz, stride=stride)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值