标签上的文本检测

时间:2021-04-21 10:53:36

标签: python opencv object-detection

我是新会员。 我对 OpenCV 感兴趣。 我想检测瓶子标签上的文字。我尝试过颜色过滤但没有成功。样本标签;

Text - LightShot SS

No Text - LightShot SS

还有我使用的代码。

import cv2  import numpy as np  

cap = cv2.VideoCapture(0)  

while(1):        
    _, frame = cap.read()  
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 
    lower_red = np.array([110,50,50]) 
    upper_red = np.array([130,255,255]) 
    mask = cv2.inRange(hsv, lower_red, upper_red) 
    res = cv2.bitwise_and(frame,frame, mask= mask) 
    cv2.imshow('frame',frame) 
    cv2.imshow('mask',mask) 
    cv2.imshow('res',res) 
    k = cv2.waitKey(5) & 0xFF
    if k == 27: 
        break

cv2.destroyAllWindows()  cap.release()

我使用的是 NVIDIA Jetson Nano 4GB。 我在等你的帮助。提前谢谢大家。

1 个答案:

答案 0 :(得分:1)

import pytesseract    #### Read this library documentation before running it 

import cv2
from PIL import Image

# pytesseract.pytesserct.image_to_string()
pytesseract.pytesseract.tesseract_cmd = r'C:\XXXX\XXXXXX\XXXX\XXXX\XXXXX\Tesseract-OCR\tesseract.exe'
img = cv2.imread('Text_entropy.png')

himg, wimg, vimg = img.shape
boxes = pytesseract.image_to_boxes(img)
for b in boxes.splitlines():
#     print(b)
    b = b.split(' ')
#     print(b)
    x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
    cv2.rectangle(img, (x,himg-y), (w, himg-h), (0, 0, 255), 1)
    cv2.putText(img, b[0], (x, himg-y+30), cv2.FONT_HERSHEY_COMPLEX, 1, (50, 50, 255), 1)
    
cv2.imshow('Result', img)


cv2.waitKey(0)

希望此代码对您有所帮助enter image description here