छवियों में मौजूद सभी अल्फ़ान्यूमेरिक पाठ की पहचान करने के लिए YOLO या अन्य छवि पहचान तकनीकों का उपयोग करना


12

मेरे पास कई चित्र आरेख हैं, जिनमें सभी में केवल पाठ लेबल के बजाय अल्फ़ान्यूमेरिक वर्णों के रूप में लेबल हैं। मैं चाहता हूं कि मेरा YOLO मॉडल इसमें मौजूद सभी संख्याओं और अल्फ़ान्यूमेरिक वर्णों की पहचान करे।

मैं अपने YOLO मॉडल को ऐसा करने के लिए कैसे प्रशिक्षित कर सकता हूं। डेटासेट यहां पाया जा सकता है। https://drive.google.com/open?id=1iEkGcreFaBIJqUdAADDXJbUrSj99bvoi

उदाहरण के लिए: बाउंडिंग बॉक्स देखें। मैं चाहता हूं कि योलो जहां भी पाठ मौजूद है, उसका पता लगाए। हालाँकि वर्तमान में इसके पाठ की पहचान करना आवश्यक नहीं है।

यहाँ छवि विवरण दर्ज करें

इस प्रकार की छवियों के लिए भी ऐसा करने की आवश्यकता है यहाँ छवि विवरण दर्ज करें यहाँ छवि विवरण दर्ज करें

चित्र यहाँ डाउनलोड किए जा सकते हैं

यह मैंने opencv का उपयोग करने की कोशिश की है, लेकिन यह डेटासेट में सभी छवियों के लिए काम नहीं करता है।

import cv2
import numpy as np
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Users\HPO2KOR\AppData\Local\Tesseract-OCR\tesseract.exe"

image = cv2.imread(r'C:\Users\HPO2KOR\Desktop\Work\venv\Patent\PARTICULATE DETECTOR\PD4.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
clean = thresh.copy()

horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(clean, [c], -1, 0, 3)

vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,30))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(clean, [c], -1, 0, 3)

cnts = cv2.findContours(clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area < 100:
        cv2.drawContours(clean, [c], -1, 0, 3)
    elif area > 1000:
        cv2.drawContours(clean, [c], -1, 0, -1)
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    x,y,w,h = cv2.boundingRect(c)
    if len(approx) == 4:
        cv2.rectangle(clean, (x, y), (x + w, y + h), 0, -1)

open_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))
opening = cv2.morphologyEx(clean, cv2.MORPH_OPEN, open_kernel, iterations=2)
close_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,2))
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, close_kernel, iterations=4)
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    area = cv2.contourArea(c)
    if area > 500:
        ROI = image[y:y+h, x:x+w]
        ROI = cv2.GaussianBlur(ROI, (3,3), 0)
        data = pytesseract.image_to_string(ROI, lang='eng',config='--psm 6')
        if data.isalnum():
            cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
            print(data)

cv2.imwrite('image.png', image)
cv2.imwrite('clean.png', clean)
cv2.imwrite('close.png', close)
cv2.imwrite('opening.png', opening)
cv2.waitKey()

क्या कोई मॉडल या कोई ओपेकेंव तकनीक या कुछ पूर्व प्रशिक्षित मॉडल है जो मेरे लिए ऐसा कर सकते हैं? मुझे बस छवियों में मौजूद सभी अल्फ़ान्यूमेरिक वर्णों के चारों ओर बाउंडिंग बॉक्स चाहिए। उसके बाद मुझे इसमें मौजूद व्हाट्स को पहचानने की जरूरत है। हालाँकि वर्तमान में दूसरा भाग महत्वपूर्ण नहीं है।


क्या इससे आपके सवाल का जवाब मिलता है? OpenCV! _Src.empty () फ़ंक्शन में 'cvtColor' त्रुटि
अमित यादव


वह सभी चित्रों के लिए काम नहीं करता है
पुलकित भटनागर

जवाबों:


7

एक संभावित दृष्टिकोण है, एओएएस (कुशल और सटीक दृश्य पाठ) का उपयोग करना है जो झोउ एट अल के 2017 के पेपर, ईएएसटी: एक कुशल और सटीक दृश्य पाठ डिटेक्टर पर आधारित गहन शिक्षण पाठ डिटेक्टर है । मॉडल को मूल रूप से प्राकृतिक दृश्य चित्रों में पाठ का पता लगाने के लिए प्रशिक्षित किया गया था, लेकिन इसे आरेख चित्रों पर लागू करना संभव हो सकता है। EAST काफी मजबूत है और धुंधला या परावर्तक पाठ का पता लगाने में सक्षम है। यहां एड्रियन रोजब्रुक के पूर्व के कार्यान्वयन का एक संशोधित संस्करण है। टेक्स्ट डिटेक्टर को सीधे इमेज पर लगाने के बजाय, हम टेक्स्ट डिटेक्शन करने से पहले इमेज पर मौजूद गैर-टेक्स्ट ऑब्जेक्ट्स को हटाने की कोशिश कर सकते हैं। पता लगाने से पहले क्षैतिज लाइनों, ऊर्ध्वाधर लाइनों और गैर-पाठ आकृति (घटता, विकर्ण, परिपत्र आकार) को हटाने का विचार है। यहां आपकी कुछ छवियों के साथ परिणाम दिए गए हैं:

इनपुट ->नॉन-टेक्स्ट कंट्रोल्स को हरे रंग में हटाने के लिए

परिणाम

अन्य चित्र

frozen_east_text_detection.pbपाठ का पता लगाने के लिए आवश्यक ढाँचा मॉडल यहाँ पाया जा सकता है । हालांकि मॉडल अधिकांश पाठ को पकड़ता है, लेकिन परिणाम 100% सटीक नहीं होते हैं और कभी-कभार झूठी सकारात्मकता होती है कि शायद यह प्राकृतिक दृश्य छवियों पर कैसे प्रशिक्षित किया गया था। अधिक सटीक परिणाम प्राप्त करने के लिए आपको संभवतः अपने स्वयं के कस्टम मॉडल को प्रशिक्षित करना होगा। लेकिन अगर आप एक अच्छा आउट-ऑफ-द-बॉक्स समाधान चाहते हैं तो यह आपको काम करना चाहिए। EAST टेक्स्ट डिटेक्टर की अधिक व्यापक व्याख्या के लिए एड्रियन ओपनसीवी टेक्स्ट डिटेक्शन (ईएएसटी टेक्स्ट डिटेक्टर) ब्लॉग पोस्ट देखें।

कोड

from imutils.object_detection import non_max_suppression
import numpy as np
import cv2

def EAST_text_detector(original, image, confidence=0.25):
    # Set the new width and height and determine the changed ratio
    (h, W) = image.shape[:2]
    (newW, newH) = (640, 640)
    rW = W / float(newW)
    rH = h / float(newH)

    # Resize the image and grab the new image dimensions
    image = cv2.resize(image, (newW, newH))
    (h, W) = image.shape[:2]

    # Define the two output layer names for the EAST detector model that
    # we are interested -- the first is the output probabilities and the
    # second can be used to derive the bounding box coordinates of text
    layerNames = [
        "feature_fusion/Conv_7/Sigmoid",
        "feature_fusion/concat_3"]

    net = cv2.dnn.readNet('frozen_east_text_detection.pb')

    # Construct a blob from the image and then perform a forward pass of
    # the model to obtain the two output layer sets
    blob = cv2.dnn.blobFromImage(image, 1.0, (W, h), (123.68, 116.78, 103.94), swapRB=True, crop=False)
    net.setInput(blob)
    (scores, geometry) = net.forward(layerNames)

    # Grab the number of rows and columns from the scores volume, then
    # initialize our set of bounding box rectangles and corresponding
    # confidence scores
    (numRows, numCols) = scores.shape[2:4]
    rects = []
    confidences = []

    # Loop over the number of rows
    for y in range(0, numRows):
        # Extract the scores (probabilities), followed by the geometrical
        # data used to derive potential bounding box coordinates that
        # surround text
        scoresData = scores[0, 0, y]
        xData0 = geometry[0, 0, y]
        xData1 = geometry[0, 1, y]
        xData2 = geometry[0, 2, y]
        xData3 = geometry[0, 3, y]
        anglesData = geometry[0, 4, y]

        # Loop over the number of columns
        for x in range(0, numCols):
            # If our score does not have sufficient probability, ignore it
            if scoresData[x] < confidence:
                continue

            # Compute the offset factor as our resulting feature maps will
            # be 4x smaller than the input image
            (offsetX, offsetY) = (x * 4.0, y * 4.0)

            # Extract the rotation angle for the prediction and then
            # compute the sin and cosine
            angle = anglesData[x]
            cos = np.cos(angle)
            sin = np.sin(angle)

            # Use the geometry volume to derive the width and height of
            # the bounding box
            h = xData0[x] + xData2[x]
            w = xData1[x] + xData3[x]

            # Compute both the starting and ending (x, y)-coordinates for
            # the text prediction bounding box
            endX = int(offsetX + (cos * xData1[x]) + (sin * xData2[x]))
            endY = int(offsetY - (sin * xData1[x]) + (cos * xData2[x]))
            startX = int(endX - w)
            startY = int(endY - h)

            # Add the bounding box coordinates and probability score to
            # our respective lists
            rects.append((startX, startY, endX, endY))
            confidences.append(scoresData[x])

    # Apply non-maxima suppression to suppress weak, overlapping bounding
    # boxes
    boxes = non_max_suppression(np.array(rects), probs=confidences)

    # Loop over the bounding boxes
    for (startX, startY, endX, endY) in boxes:
        # Scale the bounding box coordinates based on the respective
        # ratios
        startX = int(startX * rW)
        startY = int(startY * rH)
        endX = int(endX * rW)
        endY = int(endY * rH)

        # Draw the bounding box on the image
        cv2.rectangle(original, (startX, startY), (endX, endY), (36, 255, 12), 2)
    return original

# Convert to grayscale and Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
clean = thresh.copy()

# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(clean, [c], -1, 0, 3)

# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,30))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(clean, [c], -1, 0, 3)

# Remove non-text contours (curves, diagonals, circlar shapes)
cnts = cv2.findContours(clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 1500:
        cv2.drawContours(clean, [c], -1, 0, -1)
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    x,y,w,h = cv2.boundingRect(c)
    if len(approx) == 4:
        cv2.rectangle(clean, (x, y), (x + w, y + h), 0, -1)

# Bitwise-and with original image to remove contours
filtered = cv2.bitwise_and(image, image, mask=clean)
filtered[clean==0] = (255,255,255)

# Perform EAST text detection
result = EAST_text_detector(image, filtered)

cv2.imshow('filtered', filtered)
cv2.imshow('result', result)
cv2.waitKey()

बहुत पूरा जवाब। कितने घंटे का प्रयास?
कार्लफिलिप

लगभग एक घंटे और एक और 30 मिनट इसे लिखने के लिए
nathancy

आज तक मैं उन लोगों की संख्या को आश्चर्यचकित कर रहा हूं जो कुछ ही दिनों में सीवी के समान सवालों के साथ दिखाते हैं। यह लगभग उसी तरह दिखता है जैसे एक ही इमेज प्रोसेसिंग क्लास के लोग अपना होमवर्क खत्म करने के लिए मदद मांग रहे हैं या सिर्फ किसी को उनके लिए होमवर्क करने के लिए देख रहे हैं। यह वास्तव में विचित्र "संयोग" है।
कार्लफिलिप

2
@karlphillip शायद यह प्रश्न परिचित लग रहा है क्योंकि ओपी ने इसे लगभग एक सप्ताह पहले पोस्ट किया था। वह बहुत चाहता है कि एक CTRL + C, CTRL + V उत्तर हो जो उसके सभी मामलों को सही तरीके से कवर करता है, इसलिए, मुझे लगता है कि आप इस एक ही प्रश्न को कुछ हफ़्ते में फिर से देख सकते हैं!
eldesgraciado

3
@eldesgraciado मुझे बस महसूस हुआ कि ओपी ने कुछ हफ्ते पहले इसी तरह का सवाल पोस्ट किया था। यह भी पता नहीं था कि यह अब तक एक ही व्यक्ति था! मैं भी सोच रहा था क्यों सवाल देखा बहुत परिचित
nathancy

6

सुविधा के लिए मैं पैकेज keras_ocr जोड़ना चाहूंगा । इसे आसानी से पाइप के साथ स्थापित किया जा सकता है, और CRAFT टेक्स्ट डिटेक्टर पर आधारित है, जो कि EAST डिटेक्टर की तुलना में थोड़ा नया है अगर मैं गलत हूं।

पता लगाने के बगल में यह पहले से ही कुछ ओसीआर भी करता है! परिणाम नीचे दिए गए हैं, इसे एक विकल्प के रूप में देखें, शायद स्वीकार किए गए उत्तर की तुलना में लागू करना आसान है।यहाँ छवि विवरण दर्ज करें


हाय विजेता, क्या यह मेरी छवियों के कम से कम 70% के लिए काम करता है?
पुलकित भटनागर

आपने अपने डेटासेट में लेबल शामिल नहीं किए हैं। इसलिए मैं वास्तव में आपको यह नहीं बता सकता कि छवियों का कितना% इस पर काम करता है, अगर मेरे पास यह सत्यापित करने का कोई तरीका नहीं है कि क्या यह एक लेबल से तुलना करके काम करता है। हालाँकि, यह एक पाइप पैकेज है, इसलिए आपके लिए इसे अपने डेटासेट पर चलाना और अपने आप को देखना आसान होना चाहिए :)
विक्टर सोनकर

4

आप जो वर्णन कर रहे हैं वह OCR ( ऑप्टिकल कैरेक्टर रिकग्निशन ) प्रतीत होता है । एक ओसीआर इंजन जो मुझे पता है कि यह टेसेरैक्ट है , हालांकि यह आईबीएम और अन्य से भी एक है

चूंकि YOLO को मूल रूप से एक बहुत ही अलग कार्य के लिए प्रशिक्षित किया गया था, इसलिए स्थानीयकरण पाठ के लिए इसका उपयोग करने के लिए इसे खरोंच से वापस लेने की आवश्यकता होगी। जमीनी सच्चाई के लिए मौजूदा पैकेज (आपकी विशिष्ट सेटिंग के अनुकूल) का उपयोग करने का प्रयास किया जा सकता है (हालांकि यह याद रखने योग्य है कि मॉडल आमतौर पर केवल जमीनी सच्चाई के रूप में अच्छा होगा)। या, शायद अधिक आसानी से, प्रशिक्षण के लिए सिंथेटिक डेटा जनरेट करें (यानी मौजूदा ड्रॉ में चुने गए पदों में पाठ जोड़ें, फिर इसे स्थानीय करने के लिए प्रशिक्षित करें)।

वैकल्पिक रूप से, यदि आपके सभी लक्षित चित्र उपरोक्त के समान संरचित हैं, तो आप क्लासिक CV हेयुरिस्टिक्स का उपयोग करके जमीनी सच्चाई बनाने की कोशिश कर सकते हैं जैसा कि आपने प्रतीकों को अलग / खंडित करने के लिए किया था, इसके बाद MNIST पर प्रशिक्षित CNN का उपयोग करके या इसके समान निर्धारित करने के लिए वर्गीकरण द्वारा। अगर किसी दिए गए बूँद में प्रतीक है।

मामले के लिए आप योलो का विकल्प चुनते हैं - अजगर में मौजूदा कार्यान्वयन हैं, उदाहरण के लिए मुझे इस एक के साथ कुछ अनुभव था - अपने स्वयं के जमीनी सच्चाई के साथ प्रशिक्षण स्थापित करने के लिए काफी सरल होना चाहिए।

अंत में, यदि YOLO या CNN का उपयोग करना अपने आप में एक लक्ष्य नहीं है, बल्कि केवल समाधान है, तो उपरोक्त "जमीनी सच्चाई" में से कोई भी एक समाधान के रूप में सीधे इस्तेमाल किया जा सकता है, और एक मॉडल के प्रशिक्षण के लिए नहीं।

आशा है कि मैंने आपके प्रश्न को सही ढंग से समझा


यदि आप उसी के लिए कोड दे सकते हैं, क्योंकि इस प्रश्न में इनाम है
पुलकित भटनागर

कार्य अंत में पाठ प्राप्त करना है, लेकिन मैं पहले इसमें सभी अल्फ़ान्यूमेरिक वर्णों को पहचानने की कोशिश कर रहा हूं, फिर एक बार पहचाने जाने के लिए ओसीआर का उपयोग करें
पुलकित भटनागर

जो मैंने प्रस्तावित किया था, उनमें से कोई भी वास्तव में एक आउट-ऑफ-द-बॉक्स समाधान नहीं है, और एल्गोरिथम कोड मेरे विचार से छोटा या सरल नहीं होगा, इसलिए मैं इसे विचारों के स्तर पर छोड़ दूंगा :-)। ps upvote के लिए धन्यवाद!
यूरी फेल्डमैन
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.