【问题标题】:Read all images from subfolder, detect text them and save them with their original name and folder in Python?从子文件夹中读取所有图像,检测文本并在 Python 中使用原始名称和文件夹保存它们?
【发布时间】:2021-08-31 00:07:06
【问题描述】:

我有一个 train 文件夹,它包含 20 个类,每个类包含 .png 和 .jpg 格式的不同图像。我已经在一个图像上进行了测试,它工作正常,现在我想通过维护文件夹和类结构将这个过程应用于所有图像。在检测到文本之后,我们有一个名为“最终”名称的文件夹,它包含 20 个类,每个类内部都包含一个检测到的文本图像。检测到的文本代码已经可以正常工作了

代码

my_path = "Path"

files = glob.glob(my_path + '/**/*.jpg', recursive=True)
images = []

each_image = ''
for root, dirs, files in os.walk(my_path, topdown=False):
    for file in files:
        each_image = file
        print(root)
        print(dirs)

for file in files:

    image_path = file

    with open(image_path, 'rb') as image_file:
        content = image_file.read()

    image = vision.Image(content=content)
    response = client.text_detection(image=image)
    texts = response.text_annotations

    img_openCV = cv2.imread(image_path)
    h, w = img_openCV.shape[:2]
    print(h, w)

    for text in texts:
        box_w = abs(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
        box_h = abs(text.bounding_poly.vertices[2].y - text.bounding_poly.vertices[0].y)

        if box_h * box_w > thres * h * w:
            continue
        if (text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x) > (w / 2):
            print('ebug width ')
            print(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
        else:
            imagee = cv2.rectangle(img_openCV, (text.bounding_poly.vertices[0].x, text.bounding_poly.vertices[0].y),
                                   (text.bounding_poly.vertices[2].x, text.bounding_poly.vertices[2].y), (0, 255, 0),
                                   -1)
            print('height, width, color:', text.bounding_poly)
            cv2.imwrite(
                os.path.join("Path", each_image + str(total_images) + '.png'),
                imagee)
            total_images += 1

【问题讨论】:

    标签: python python-3.x list glob subdirectory


    【解决方案1】:
    import os, io
    import random
    from turtle import color
    
    from google.cloud import vision
    import numpy as np
    from PIL import Image, ImageDraw, ImageFont
    import cv2
    from text import draw_borders
    import glob
    
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "GoogleVisionAPI.json"
    client = vision.ImageAnnotatorClient()
    
    # file_name = '3017.jpg'
    # image_folder = './img/'
    
    total_images = 1
    thres = 0.5
    
    # Location with subdirectories
    my_path = "/Path of whole folder which contains subclasses/"
    
    files = glob.glob(my_path + '/**/*.jpg', recursive=True)
    images = []
    
    each_image = ''
    # for root, dirs, files in os.walk(my_path, topdown=False):
    #    for file in files:
    #        each_image = file
    #        print(root)
    #        print(dirs)
    
    for root, dirs, files in os.walk(my_path, topdown=False):
        for file in files:
            image_path = file
    
            with open(root + "/" + image_path, 'rb') as image_file:
                content = image_file.read()
    
                image = vision.Image(content=content)
                response = client.text_detection(image=image)
                texts = response.text_annotations
    
                img_openCV = cv2.imread(root + "/" + image_path)
                h, w = img_openCV.shape[:2]
                print(h, w)
    
                for text in texts:
                    box_w = abs(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
                    box_h = abs(text.bounding_poly.vertices[2].y - text.bounding_poly.vertices[0].y)
    
                    if box_h * box_w > thres * h * w:
                        continue
                    if (text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x) > (w / 2):
                        print('ebug width ')
                        print(text.bounding_poly.vertices[2].x - text.bounding_poly.vertices[0].x)
                    else:
                        imagee = cv2.rectangle(img_openCV,
                                               (text.bounding_poly.vertices[0].x, text.bounding_poly.vertices[0].y),
                                               (text.bounding_poly.vertices[2].x, text.bounding_poly.vertices[2].y),
                                               (0, 255, 0),
                                               -1)
                        print('height, width, color:', text.bounding_poly)
                        if root.__contains__("BC"):
                            cv2.imwrite(os.path.join("/Specific_class", file), imagee)
                        elif root.__contains__("BK"):
                            cv2.imwrite(os.path.join("/Specific_class", file), imagee)
                        elif root.__contains__("CC"):
                            cv2.imwrite(os.path.join("/Specific_class", file), imagee)
                        
                 
                total_images += 1
    
    # cv2.imshow('window_name', imagee)
    # cv2.waitKey(0)
    
    # closing all open windows
    # cv2.destroyAllWindows()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-29
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多