Core ML模型预测报错:imagePath需为PIL.Image.Image类型求解决方案
问题
我有一个针对1920x1080分辨率车牌训练的.mlmodel模型,测试视频分辨率同样为1920x1080。运行代码时出现如下错误:
typeerror: Image input, 'imagePath' must be of type PIL.Image.Image in the input dict
报错代码行:
# Predict using Core ML model output = model.predict({"imagePath": temp.name, "iouThreshold": 0.5, "confidenceThreshold": 0.5})
完整代码如下:
import cv2 model = coremltools.models.MLModel('NeuralVision.mlmodel') video_path = 'anpr_video.mp4' cap = cv2.VideoCapture(video_path) # Desired size for the model input new_width = 1920 new_height = 1080 # Process the video frame by frame while cap.isOpened(): ret, frame = cap.read() # Break the loop if we have reached the end of the video if not ret: break # Convert the frame to a PIL Image image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # Resize the image to the desired size resized_image = image.resize((new_width, new_height), Image.LANCZOS) # Save the resized image as a temporary file with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp: resized_image.save(temp.name) # Predict using Core ML model output = model.predict({"imagePath": temp.name, "iouThreshold": 0.5, "confidenceThreshold": 0.5}) # Print the output print(output) # Display the frame cv2.imshow('Frame', frame) # Break the loop if the user presses the 'q' key if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture object and close all windows cap.release() cv2.destroyAllWindows()
解决方法
错误原因清晰:你的Core ML模型要求imagePath输入是PIL.Image.Image类型,但你传入的是临时文件路径字符串,触发了类型错误。直接传入处理好的PIL图像即可,无需存临时文件,修改步骤如下:
- 删除临时文件相关代码:不需要将PIL图像存为文件再读取,直接使用内存中的图像对象
- 修改
predict的输入参数:把temp.name替换为处理好的resized_image - 补上缺失的模块导入:原代码缺少
from PIL import Image、import tempfile和import coremltools语句,必须添加
修改后的完整代码:
import cv2 from PIL import Image import tempfile import coremltools model = coremltools.models.MLModel('NeuralVision.mlmodel') video_path = 'anpr_video.mp4' cap = cv2.VideoCapture(video_path) # Desired size for the model input new_width = 1920 new_height = 1080 # Process the video frame by frame while cap.isOpened(): ret, frame = cap.read() # Break the loop if we have reached the end of the video if not ret: break # Convert the frame to a PIL Image image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # Resize the image to the desired size resized_image = image.resize((new_width, new_height), Image.LANCZOS) # Predict using Core ML model - 直接传入PIL图像对象 output = model.predict({"imagePath": resized_image, "iouThreshold": 0.5, "confidenceThreshold": 0.5}) # Print the output print(output) # Display the frame cv2.imshow('Frame', frame) # Break the loop if the user presses the 'q' key if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture object and close all windows cap.release() cv2.destroyAllWindows()
内容的提问来源于stack exchange,提问作者Elise siera
相关产品推荐
相关产品推荐

