[英]Flask: TypeError: Expected Ptr<cv::UMat> for argument '%s'
[英]TypeError: Expected Ptr<cv::UMat> for argument '%s'
我有这个我自己无法解决的错误。
你能告诉我如何解决吗?
问题出在这个 function 中:
import numpy as np
import cv2
import matplotlib.pyplot as plt
def histogram_of_pixel_projection(img):
# list that will contains all digits
caracrter_list_image = list()
# img = crop(img)
# Add black border to the image
BLACK = [0, 0, 0]
#img = cv2.copyMakeBorder(img, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=BLACK)
img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_CONSTANT)
# change to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Change to numpy array format
nb = np.array(gray)
# Binarization
nb[nb > 120] = 255
nb[nb < 120] = 0
# compute the sommation
x_sum = cv2.reduce(nb, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
y_sum = cv2.reduce(nb, 1, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
# rotate the vector x_sum
x_sum = x_sum.transpose()
# get height and weight
x = gray.shape[1]
y = gray.shape[0]
# division the result by height and weight
x_sum = x_sum / y
y_sum = y_sum / x
# x_arr and y_arr are two vector weight and height to plot histogram projection properly
x_arr = np.arange(x)
y_arr = np.arange(y)
# convert x_sum to numpy array
z = np.array(x_sum)
# convert y_arr to numpy array
w = np.array(y_sum)
# convert to zero small details
z[z < 15] = 0
z[z > 15] = 1
# convert to zero small details and 1 for needed details
w[w < 20] = 0
w[w > 20] = 1
# vertical segmentation
test = z.transpose() * nb
# horizontal segmentation
test = w * test
# plot histogram projection result using pyplot
horizontal = plt.plot(w, y_arr)
vertical = plt.plot(x_arr ,z)
plt.show(horizontal)
plt.show(vertical)
f = 0
ff = z[0]
t1 = list()
t2 = list()
for i in range(z.size):
if z[i] != ff:
f += 1
ff = z[i]
t1.append(i)
rect_h = np.array(t1)
f = 0
ff = w[0]
for i in range(w.size):
if w[i] != ff:
f += 1
ff = w[i]
t2.append(i)
rect_v = np.array(t2)
# take the appropriate height
rectv = []
rectv.append(rect_v[0])
rectv.append(rect_v[1])
max = int(rect_v[1]) - int(rect_v[0])
for i in range(len(rect_v) - 1):
diff2 = int(rect_v[i + 1]) - int(rect_v[i])
if diff2 > max:
rectv[0] = rect_v[i]
rectv[1] = rect_v[i + 1]
max = diff2
# extract caracter
for i in range(len(rect_h) - 1):
# eliminate slice that can't be a digit, a digit must have width bigger then 8
diff1 = int(rect_h[i + 1]) - int(rect_h[i])
if (diff1 > 5) and (z[rect_h[i]] == 1):
# cutting nb (image) and adding each slice to the list caracrter_list_image
caracrter_list_image.append(nb[int(rectv[0]):int(rectv[1]), rect_h[i]:rect_h[i + 1]])
# draw rectangle on digits
cv2.rectangle(img, (rect_h[i], rectv[0]), (rect_h[i + 1], rectv[1]), (0, 255, 0), 1)
# Show segmentation result
image = plt.imshow(img)
plt.show(image)
return caracrter_list_image
img='00.PNG'
histogram_of_pixel_projection(img)
该代码应该拍摄图像,然后执行以下操作:
当我运行代码时,我收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-eb57438d5985> in <module>()
184 img='00.PNG'
--> 185 histogram_of_pixel_projection(img)
<ipython-input-17-eb57438d5985> in histogram_of_pixel_projection(img)
72 BLACK = [0, 0, 0]
73 #img = cv2.copyMakeBorder(img, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=BLACK)
---> 74 img = cv2.copyMakeBorder(img, 10, 10, 10, 10, cv2.BORDER_CONSTANT)
75
76 # change to gray
TypeError: Expected Ptr<cv::UMat> for argument '%s'
编辑:我添加了运行代码所需的导入。
注意:要能够import cv2
,您需要pip install opencv-python
#代码
**从 sklearn.preprocessing 导入 LabelEncoder 从 keras.models 导入 model_from_json 导入 numpy 作为 np
json_file = open('Caracter_Recognition_Train_Model/MobileNets_character_recognition.json', 'r') loaded_model_json = json_file.read() json_file.close()
model = model_from_json(loaded_model_json)# Load the digit prediction model model.load_weights("Caracter_Recognition_Train_Model/License_character_recognition.h5") print("[INFO] Model loaded successfully...")
labels = LabelEncoder() labels.classes_ = np.load('Caracter_Recognition_Train_Model/license_character_classes.npy') print("[INFO] 标签加载成功...") **
#错误
** Traceback(最近一次调用最后):文件“C:/Users/Mahdia/PycharmProjects/ ProjetFinale _/Caracter_Recognition_Avc_Modele.py”,第 1 行,从 sklearn.preprocessing 导入 LabelEncoder 文件“C:\Users\Mahdia\PycharmProjects_ ProjetFinale __\venv\lib\site-packages\sklearn_ init _.py”,第 82 行,在 from.base 导入克隆文件“C:\Users\Mahdia\ PycharmProjects_ProjetFinale __\venv\lib\site-packages\sklearn\base .py”,第 17 行,从.utils 导入IS_32BIT 文件“C:\Users\Mahdia\PycharmProjects_ ProjetFinale _\venv\lib\site-packages\sklearn\ utils_init _.py”,第 20 行,来自 scipy。稀疏导入 issparse 文件“C:\Users\Mahdia\ PycharmProjects_ProjetFinale __\venv\lib\site-packages\scipy_init _.py ”,第 130 行,来自。 在WinDLL (os.path.abspath(filename)) 文件“C:\Program Files \WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\ctypes_ init _.py",第 364 行,在init self._handle = _dlopen(self._name, mode) OSError: [Winéest 1666 模块可打扰的**
当它应该是一个数组图像时,您正在发送一个字符串变量,请使用以下内容:
img = cv2.imread('00.PNG')
histogram_of_pixel_projection(img)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.