Figure and number recognition about CV2

dailiben 注册会员
2023-02-25 23:25

Part of the answer refers to GPT, GPT_Pro better solve the problem
CNN convolutional neural network can be used for image recognition. CNN is a special neural network structure, which can automatically extract the features of the image to identify the numbers and shapes in the image. In addition, CNN can identify numbers and graphics separately and output results, which can greatly improve the accuracy.

How to use CNN for image recognition?

  1. First, we need to convert the original image into input samples that can be used by CNN, which generally include standardization, normalization, cropping, cropping, rotation and expansion, etc.
  2. Then, we need to train CNN to recognize numbers and shapes in the image. There are many ways to train CNN, and we can choose the best method according to the specific situation.
  3. Finally, we can predict the test image. In the prediction, we need to match the test image with the trained CNN model, and output serial number + number + figure according to the predicted number and shape, such as '1+2+ triangle '.

Here is an example of Python code that uses CNN for number and graph recognition:

# 导入所需要的库
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Dense, Flatten
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam

# 定义模型
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# 编译模型
model.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy'])

# 数据集准备
train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2)
test_datagen = ImageDataGenerator(rescale=1./255)  #测试集不能进行随机处理
train_set = train_datagen.flow_from_directory('dataset/training_set', target_size=(64, 64), batch_size=32)  #注意此处为文件夹目录
test_set = test_datagen.flow_from_directory('dataset/test_set', target_size=(64, 64), batch_size=32)   #注意此处为文件夹目录

# 训练模型
model.fit_generator(train_set, epochs=25, steps_per_epoch=8000/32)  #注意此处为样本数/batch size 

If the answer is helpful, please accept it.

cx8311841 注册会员
2023-02-25 23:25

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > can increase the recognition accuracy of the method include:

  1. Using more advanced image processing technology, such as edge detection, image segmentation, feature extraction, etc., more effective information can be extracted for recognition.

  2. Using deep learning methods, such as convolutional neural networks(CNN), cyclic neural networks(RNN), etc., can be trained according to a large number of data to improve recognition accuracy.

  3. Better image preprocessing, such as denoising, histogram equalization, filtering, etc., can improve the quality of the image and improve the accuracy of recognition.

  4. The numbers and figures can be recognized separately, so as to improve the accuracy of recognition.

Here is an example code for identifying numbers and figures separately:

import cv2
import numpy as np

# 读取图像
img = cv2.imread('image.jpg')

# 将图像转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 对图像进行二值化处理
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# 对图像进行形态学处理,填充空洞
kernel = np.ones((5,5),np.uint8)
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# 对图像进行轮廓检测
contours, hierarchy = cv2.findContours(closed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# 对每个轮廓进行处理
for i in range(len(contours)):
    # 获取轮廓的边界框
    x, y, w, h = cv2.boundingRect(contours[i])
    # 如果边界框太小,则认为不是数字或图形
    if w < 10 or h < 10:
    # 对数字和图形分别进行识别
    if w > h:  # 数字
        # 对数字进行裁剪,并调整大小
        digit = thresh[y:y+h, x:x+w]
        digit = cv2.resize(digit, (28, 28))
        # 使用训练好的模型进行预测
        digit = digit.reshape((1, 28, 28, 1))
        digit = digit.astype('float32') / 255
        result = model_digit.predict(digit)
        digit_num = np.argmax(result)
        # 输出结果
        print(str(i+1) + '+' + str(digit_num))
    else:  # 图形
        # 对图形进行裁剪,并调整大小
        shape = thresh[y:y+h, x:x+w]
        shape = cv2.resize(shape, (32, 32))
        # 使用模板匹配进行识别
        result = []
        for template in templates:
            res = cv2.matchTemplate(shape, template, cv2.TM_CCOEFF_NORMED)
        shape_type = np.argmax(result)
        # 输出结果

About the Author

Question Info

Publish Time
2023-02-25 23:25
Update Time
2023-02-25 23:25

Related Question

Android studio

如何读取json文件在本地测试的Android Studio?



Delphi IDE不将Android设备显示为构建目标



android studio 运行问题


android 双屏异显异触问题