我可以在 Visual Studio 上使用 Python 3.8 使用 Pytesseract 和 OpenCV 扫描 PNG iPhone 短信吗?

问题描述 投票:0回答:1

我只得到 Python 可执行文件的输出,显示启动脚本、检查图像 png,然后脚本完成。然后输入任意键继续。我想要的输出是扫描 png 并输出指定目录中的每个屏幕截图(.png 文件),使用 Tesseract OCR 从屏幕截图中提取文本,然后将提取的文本打印到终端或命令提示符,分类为发短信的人(“您”或“其他发短信的人”)。

import os
import cv2
import pytesseract

print("Starting script...")

# Specify the path to the Tesseract executable
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

# Define the directory path
directory_path = r'C:\Users\username\OneDrive\Documents\Convo'

# Loop through all PNG files in the directory
for filename in os.listdir(directory_path):
    print(f"Checking file: {filename}")
    if filename.endswith('.png'):
        print(f"Processing file: {filename}")
        image_path = os.path.join(directory_path, filename)
        image = cv2.imread(image_path)
        
        # Convert the image to the HSV color space
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        
        # Define the color ranges
        # Define blue color range in HSV
        lower_blue = (90, 50, 50)
        upper_blue = (130, 255, 255)

        # Define grey color range in HSV
        lower_grey = (0, 0, 150)
        upper_grey = (180, 25, 220)

        
        # Create masks for each color range
        blue_mask = cv2.inRange(hsv, lower_blue, upper_blue)
        grey_mask = cv2.inRange(hsv, lower_grey, upper_grey)
        
        # Extract and classify text
        for mask, texter in [(blue_mask, "You"), (grey_mask, "Other Texter")]:
            contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
            for contour in contours:
                x, y, w, h = cv2.boundingRect(contour)
                roi = image[y:y+h, x:x+w]
                text = pytesseract.image_to_string(roi)
                print(f"{texter}: {text}")
        
        # Print a message after processing each screenshot
        print(f"Processed {filename}")

print("Script completed.")

opencv python-3.8 python-tesseract
1个回答
0
投票

这是不久前的事了。但我想通了。我必须使用 Python 环境,安装所有库。将 500 多个图像插入所需的代码行,然后运行它并将其输出到 .txt 文件中。这只是我和妈妈之间的旧短信。我使用 Python 收集了所有最常用的单词并将它们输出到图片中。但我也将它们保存到文本文件中,并且能够读取文本的情绪、主观性、字数、消息数和其他特征。

© www.soinside.com 2019 - 2024. All rights reserved.