![](/img/trans.png)
[英]Read all images from subfolder, resize them and save them with their original name in Python?
[英]Read all images from subfolder, detect text them and save them with their original name and folder in Python?
我有一個 train 文件夾,它包含 20 個類,每個 class 包含不同的 .png 和 .jpg 格式的圖像。 我已經在一個圖像上進行了測試,它工作正常,現在我想通過維護文件夾和 class 結構來將此過程應用於所有圖像。 在檢測到文本之后,我們有一個名為“final”的文件夾,它包含 20 個類,每個 class 內部包含一個檢測到的文本圖像。 檢測到的文本代碼已經可以正常工作
代碼
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
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.