Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cv2窗口未打开_Python_Opencv_Detection - Fatal编程技术网

Python cv2窗口未打开

Python cv2窗口未打开,python,opencv,detection,Python,Opencv,Detection,我正在尝试建立一个字母预测模型,它从相机中获取输入并预测字母,但是cv2相机窗口没有打开,相机打开(我知道,因为我相机上的绿灯打开),但窗口没有打开 我的代码(有点长,对不起): 您应该删除尝试/,但除外,然后查看是否出现异常。现在,你忽略了它们。你最大的错误是除了:pass import cv2 from numpy import random import pandas as pd import numpy as np from sklearn.model_selection import

我正在尝试建立一个字母预测模型,它从相机中获取输入并预测字母,但是
cv2
相机窗口没有打开,相机打开(我知道,因为我相机上的绿灯打开),但窗口没有打开

我的代码(有点长,对不起):


您应该删除
尝试
/
,但
除外,然后查看是否出现异常。现在,你忽略了它们。你最大的错误是
除了:pass
import cv2
from numpy import random
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from PIL import Image
import PIL.ImageOps

X = np.load('image.npz')['arr_0']
y = pd.read_csv('labels.csv')["labels"]

classes = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
nClasses = len(classes)

xTrain, xTest, yTrain, yTest = train_test_split(X, y, train_size=7500, test_size=2500, random_state=9)

xTrainScaled = xTrain / 255
xTestScaled = xTest / 255

model = LogisticRegression(solver='saga', multi_class='multinomial').fit(xTrainScaled, yTrain)

yPredict = model.predict(xTestScaled)

accuracy = accuracy_score(yTest, yPredict)
print("The accuracy is: ", accuracy)

capture = cv2.VideoCapture(0)

while(True):
    try:
        ret, frame = capture.read()

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        height, width = gray.shape
        upper_left = (int(width / 2 - 60), int(height / 2 - 60))
        bottom_right = (int(width / 2 + 60), int(width / 2 + 60))
        cv2.rectangle(gray, upper_left, bottom_right, (0, 255, 0), 2)

        roi = gray[upper_left[1]:bottom_right[1], upper_left[0]:bottom_right[0]]

        im_pil = Image.fromarray(roi)

        image_bw = im_pil.convert('L')
        image_bw_resized = image_bw.resize((28, 28), Image.ANTIALIAS)

        image_bw_resized_inverted = PIL.ImageOps.invert(image_bw_resized)
        pixel_filter = 20
        min_pixel = np.percentile(image_bw_resized_inverted, pixel_filter)
        image_bw_resized_inverted_scaled = np.clip(image_bw_resized_inverted - min_pixel, 0, 255)
        max_pixel = np.max(image_bw_resized_inverted)
        image_bw_resized_inverted_scaled = np.asarray(image_bw_resized_inverted_scaled) / max_pixel
        test_sample = np.array(image_bw_resized_inverted_scaled).reshape(1, 784)
        test_pred = model.predict(test_sample)
        print("The predicted letter is: ", test_pred)

        cv2.imshow('frame', gray)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    except Exception as e:
        pass

capture.release()
cv2.destroyAllWindows()