Yolo 训练 Error in load_data_detection() - OpenCV 解决办法之一

本文介绍了解决YoloV4模型无法识别位深度为8的图片的问题,通过将图片位深度转换为24的方法,成功实现了模型的正常训练。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近使用YoloV4对验证码模型训练,发现图片位深度为8 的图片无法进行识别,报了个Cannot Load image, Error in load_data_detection() - OpenCV

 

本以为是图片路径问题,结果测试过后,路径没问题, 后发现图片的深度是8,将图片位深度转换为24后就可以正常训练了,记录一下,以下是转换代码:

import numpy as np
from PIL import Image
import os

path='img_8/'
newpath='change_24/'
def turnto24(path):
    files = os.listdir(path)
    files = np.sort(files)
    i = 0
    for f in files:
        imgpath = path + f
        img = Image.open(imgpath).convert('RGB')
        dirpath = newpath
        file_name, file_extend = os.path.splitext(f)
        dst = os.path.join(os.path.abspath(dirpath), file_name + '.jpg')
        img.save(dst)

turnto24(path)

 

以下是可以用zed相机调用yolo v7模型的Python代码示例: 首先,您需要安装zed相机SDK和yolo v7模型,并安装OpenCV和其他必要的库。然后,您可以使用以下代码: ```python import cv2 import pyzed.sl as sl import numpy as np # Load the YOLOv7 object detection model model = cv2.dnn.readNetFromDarknet("yolov7.cfg", "yolov7.weights") # Set the input size input_size = (416, 416) # Set the confidence threshold confidence_threshold = 0.5 # Open the ZED camera zed = sl.Camera() init = sl.InitParameters() init.camera_resolution = sl.RESOLUTION.HD720 init.depth_mode = sl.DEPTH_MODE.NONE init.camera_fps = 30 err = zed.open(init) if err != sl.ERROR_CODE.SUCCESS: print(repr(err)) zed.close() exit() # Capture frames from the ZED camera and resize them while True: # Grab a new image from the ZED camera runtime_parameters = sl.RuntimeParameters() left_image = sl.Mat() zed.grab(runtime_parameters) zed.retrieve_image(left_image, sl.VIEW.LEFT) # Convert the image to a format that can be processed by YOLOv7 image = cv2.resize(left_image.get_data(), input_size) blob = cv2.dnn.blobFromImage(image, 1/255.0, input_size, swapRB=True, crop=False) # Set the inputs and run the YOLOv7 model model.setInput(blob) outputs = model.forward() # Filter the outputs by confidence threshold and draw bounding boxes for o in outputs: for detection in o: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > confidence_threshold: x, y, w, h = detection[:4] * np.array([left_image.get_width(), left_image.get_height()] * 2) xmin, ymin, xmax, ymax = int(x - w/2), int(y - h/2), int(x + w/2), int(y + h/2) cv2.rectangle(left_image.get_data(), (xmin, ymin), (xmax, ymax), (255,0,0), 2) # Display the processed image cv2.imshow("YOLOv7 object detection", left_image.get_data()) # Exit loop on ESCAPE key = cv2.waitKey(1) if key == 27: break # Release the ZED camera zed.close() # Close the OpenCV window cv2.destroyAllWindows() ``` 请注意,以上代码可能需要根据您的要求进行修改。
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值