Das*_*wer 11 opencv numpy image-processing python-imaging-library
我正在尝试转换 Keras DarkNet 代码的部分内容,以使代码运行得更快。这是我试图优化的代码:
model_image_size = (416, 416)
import cv2
from PIL import Image
frame = cv2.imread("test.png", cv2.IMREAD_COLOR)
im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
im = Image.fromarray(im).crop((1625, 785, 1920, 1080)) # crop ROI
resized_image = im.resize(tuple(reversed(model_image_size)), Image.BICUBIC)
image_data = np.array(resized_image, dtype='float32')
image_data /= 255.
image_data = np.expand_dims(image_data, 0) # Add batch dimension.
return image_data
Run Code Online (Sandbox Code Playgroud)
这是我尝试在不使用中间 PIL 转换来减少时间的情况下实现相同的输出:
model_image_size = (416, 416)
import cv2
frame = cv2.imread("test.png", cv2.IMREAD_COLOR)
frame = frame[785:1080,1625:1920] # crop ROI
im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
resized_image = cv2.resize(im, model_image_size, interpolation = cv2.INTER_CUBIC)
resized_image /= 255.
image_data = np.expand_dims(resized_image, 0) # Add batch dimension.
return image_data
Run Code Online (Sandbox Code Playgroud)
但是,运行代码后,它将返回:
resized_image /= 255.
TypeError: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'B') according to the casting rule ''same_kind''
Run Code Online (Sandbox Code Playgroud)
似乎我需要在标准化之前将uint8类型更改为float32,但我不确定如何使用 OpenCV 实现它。
Anu*_*ngh 15
您可以使用resized_image.astype(np.float32)将resized_image数据从转换unit8为float32,然后继续进行标准化和其他操作:
frame = cv2.imread("yourfile.png")
frame = frame[200:500,400:1000] # crop ROI
im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
model_image_size = (416, 416)
resized_image = cv2.resize(im, model_image_size, interpolation = cv2.INTER_CUBIC)
resized_image = resized_image.astype(np.float32)
resized_image /= 255.
image_data = np.expand_dims(resized_image, 0) # Add batch dimension.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41769 次 |
| 最近记录: |