您当前的位置:首页 > 计算机 > 编程开发 > Python

截图识别图片中的文字

时间:07-03来源:作者:点击数:

截图识别图片中的文字

import logging
import os
import re
import sys
import time
import warnings
from os import listdir
from PIL import Image
import cv2  # pip3.8 install opencv-python
# import matplotlib
import matplotlib.pyplot as plt  # pip3.8 install matplotlib
import paddlehub as hub  # pip3.8 install paddlehub   python3.8 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
import pandas as pd  # pip3.8 install pandas
import pymysql  # pip3.8 install pymysql
import urllib3
from PIL import Image  # pip3.8 install Pillow
 
# matplotlib.use('agg')  # 'Qt5Agg', 'TkAgg', 'agg'
urllib3.disable_warnings()
warnings.filterwarnings("ignore")
pd.set_option('display.max_columns', None)  # 显示的最大行数  None:全部显示
pd.set_option('display.max_rows', None)  # 显示的最大列数  None:全部显示
pd.set_option('expand_frame_repr', False)  # True就是可以换行显示。设置成False的时候不允许换行
pd.set_option('display.width', 200)  # 横向最多显示多少个字符, 一般80不适合横向的屏幕,平时多用200.
pd.set_option('precision', 0)
path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(path)
from spider_setting import MYSQL_HOST, MYSQL_POST, MYSQL_PASSWORD, MYSQL_USER
 
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# BASE_DIR = "Z:\\image"
logging.basicConfig(
    level=logging.INFO,  # 定义输出到文件的log级别,大于此级别的都被输出
    format='%(asctime)s  %(filename)s  %(levelname)s : %(message)s',  # 定义输出log的格式
    datefmt='%Y-%m-%d %H:%M:%S',  # 时间
    filename="{}\\pfs_ping.log".format(BASE_DIR),
    filemode='a')  # 写入模式“w”或“a”
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# 设置格式
formatter = logging.Formatter('%(asctime)s  %(filename)s  %(levelname)s : %(message)s')
# 告诉handler使用这个格式
console.setFormatter(formatter)
# 为root logger添加handler
logging.getLogger('').addHandler(console)
logging.info("wwwwwwwwwwwww")
 
 
class GetFps(object):
    def __init__(self):
        self.db = pymysql.connect(host=MYSQL_HOST, port=MYSQL_POST, database="cloud_joy_monitoring", user=MYSQL_USER, password=MYSQL_PASSWORD, charset='utf8', autocommit=True)
        self.cursor = self.db.cursor()
        self.create_time = time.strftime('%Y-%m-%d %X')
        self.status = False
        self.table_name = "game_delay"
        self.IMAGES_FORMAT = ['.jpg', '.JPG']  # 图片格式
        self.IMAGE_ROW = 1  # 图片间隔,也就是合并成一张图后,一共有几行
        self.IMAGE_COLUMN = 2  # 图片间隔,也就是合并成一张图后,一共有几列
 
    def get_image(self):  # 获取图片
        for self.video_path in ["Z:\\20211110\\水炮炮\\CSGO"]:  # "Z:\\20211110\\火炮炮\\CSGO",
            for video_name in os.listdir(self.video_path):
                if "mp4" in video_name:
                    print(video_name)
                    self.video_name = video_name
                    # video_capture = cv2.VideoCapture("{}\\videos\\{}".format(BASE_DIR, video_name))
                    video_capture = cv2.VideoCapture("{}\\{}".format(self.video_path, video_name))
                    for i in range(500, 1000000000, 100):
                        num = self.cursor.execute('select id from game_record where number={} and video_name="{}"'.format(i, self.video_name))
                        num = 0
                        if not num:
                            insert_sql = 'insert into game_record(number, video_name) values({}, "{}")'.format(i, self.video_name)
                            self.cursor.execute(insert_sql)
                            num = self.cursor.execute('select id from {} where number={} and video_name="{}" and ping is not null'.format(self.table_name, i, self.video_name))
                            if not num:
                                video_capture.set(cv2.CAP_PROP_POS_FRAMES, i)
                                success, frame = video_capture.read()
                                if success is False:
                                    break
                                if i % 100 == 0:
                                    try:
                                        milliseconds = video_capture.get(cv2.CAP_PROP_POS_MSEC)
                                        video_time = self.get_second(milliseconds)
                                        iamge_path = '{}\\images\\{}_{}.jpg'.format(BASE_DIR, self.video_name.split(".")[0], i)
                                        cv2.imwrite(iamge_path, frame)
                                        self.screenshot(iamge_path, i, video_time)
                                        self.remove_path(iamge_path)
                                    except Exception as e:
                                        print("-------报错:{}----------".format(e))
                                cv2.waitKey(1)
 
                    video_capture.release()
                    # break
 
    def get_second(self, milliseconds):
        seconds = milliseconds // 1000
        milliseconds = milliseconds % 1000
        minutes = 0
        hours = 0
        if seconds >= 60:
            minutes = seconds // 60
            seconds = seconds % 60
 
        if minutes >= 60:
            hours = minutes // 60
            minutes = minutes % 60
        videl_time = "{}时:{}分:{}.{}秒".format(int(hours), int(minutes), int(seconds), int(milliseconds))
        return videl_time
 
    def screenshot(self, iamge_path, index, video_time):  # 截取图片
        image = Image.open(iamge_path)
        width, height = image.size
        print(height, width)
        num = self.cursor.execute('select id from {} where number={} and video_name="{}" and ping is not null'.format(self.table_name, index, self.video_name))
        if not num:
            rangle = (1046, 945, 1707, 1045)
            jpg_index = image.crop(rangle)
            iamge_path = '{}\\image\\{}_{}_#.jpg'.format(BASE_DIR, self.video_name.split(".")[0], index)
            jpg_index.save(iamge_path)
            rangle = (1455, 951, 1549, 980)
            jpg_index = image.crop(rangle)
            iamge_path_a = '{}\\img\\{}_{}_A.jpg'.format(BASE_DIR, self.video_name.split(".")[0], index)
            jpg_index.save(iamge_path_a)
            rangle = (1818, 1000, 1920, 1029)
            jpg_index = image.crop(rangle)
            iamge_path_b = '{}\\img\\{}_{}_B.jpg'.format(BASE_DIR, self.video_name.split(".")[0], index)
            jpg_index.save(iamge_path_b)
 
            to_image = Image.new('RGB', (200, 29))  # 创建一个新图
            image_names = [iamge_path_a, iamge_path_b]
            # 循环遍历,把每张图片按顺序粘贴到对应位置上
            for y in range(1, self.IMAGE_ROW + 1):
                for x in range(1, self.IMAGE_COLUMN + 1):
                    from_image = Image.open(image_names[self.IMAGE_COLUMN * (y - 1) + x - 1])  # .resize((self.IMAGE_SIZE, self.IMAGE_SIZE), Image.ANTIALIAS)
                    to_image.paste(from_image, ((x - 1) * from_image.size[0], (y - 1) * from_image.size[1]))
 
            iamge_path = '{}\\img\\{}_{}_#.jpg'.format(BASE_DIR, self.video_name.split(".")[0], index)
            to_image.save(iamge_path)  # 保存新图
 
            self.magnify_image(iamge_path, index, video_time)
            self.remove_path(iamge_path)
 
    def magnify_image(self, iamge_path, index, video_time):  # 放大图片
        img = cv2.imread(iamge_path, -1)
        enlarge = cv2.resize(img, (0, 0), fx=100, fy=100, interpolation=cv2.INTER_CUBIC)
        if "#" in str(iamge_path):
            iamge_path = '{}\\magnify\\{}_{}_#.jpg'.format(BASE_DIR, self.video_name.split(".")[0], index)
        cv2.imwrite(iamge_path, enlarge)
        self.paddlehub_image(iamge_path, index, video_time)
 
    def paddlehub_image(self, iamge_path, index, video_time):  # 识别图片内容
        ocr = hub.Module(name="chinese_ocr_db_crnn_server")  # chinese_ocr_db_crnn_mobile:简单模型    chinese_ocr_db_crnn_server:复制模型
        result = ocr.recognize_text(images=[cv2.imread(iamge_path)])
        index = []
        content_desc = {}
        content = ""
        for result_text in result[0]['data']:
            index.append(int(result_text["text_box_position"][0][0]))
            content_desc[str(result_text["text_box_position"][0][0])] = result_text['text']
        index.sort(reverse=False)
        for i in index:
            content = content + content_desc[str(i)]
        print("paddlehub_image content:{}  iamge_path:{}".format(content, iamge_path))
        self.insert_sql(content.lower(), iamge_path, index, video_time)
 
    def insert_sql(self, content, iamge_path, index, video_time):  # 数据入库
        if "m" in content or "s" in content or "n" in content:
            ping = re.findall(r'(\d+)', content)
            if ping:
                ping = ping[0]
            else:
                ping = 0
            print("ping:{}  content:{}  video_time:{}  video_name:{}".format(ping, content, video_time, self.video_name))
            num = self.cursor.execute('select id, content from {} where number={} and video_name="{}"'.format(self.table_name, index, self.video_name))
            if not num:
                try:
                    insert_sql = 'insert into {}(content, ping, platform, create_time, number, video_name, video_time) values("{}", {}, "{}", "{}", {}, "{}", "{}")'.format(
                        self.table_name, content, ping, 1, self.create_time, index, self.video_name, video_time)
                    self.cursor.execute(insert_sql)
                except:
                    print("insert_sql:{}".format(insert_sql))
            else:
                (id, contents) = self.cursor.fetchall()[0]
                update_sql = 'update {} set ping={}, content="{}" where id={}'.format(self.table_name, ping, contents + content, id)
                self.cursor.execute(update_sql)
 
    def show_image(self, number, video_name, image, symbol):
        # img = Image.open(os.path.join(BASE_DIR, 'img/{}_{}_{}'.format(video_name, number, symbol) + '.jpg'))
        img = Image.open(os.path.join(BASE_DIR, 'image/{}_{}_{}'.format(video_name, number, symbol) + '.jpg'))
        plt.figure("Image")  # 图像窗口名称
        plt.imshow(img)
        plt.axis('on')  # 关掉坐标轴为 off
        plt.title(image)  # 图像题目
        plt.show()
        # pylab.show()
 
    def revised_data(self):
        self.cursor.execute("select id, ping, content, number, video_name from {}".format(self.table_name))
        for data in self.cursor.fetchall():
            id = data[0]
            field_data = data[1]
            number = data[3]
            video_name = data[4].split(".")[0]
            if field_data < 20 or field_data > 150:
                print("{}-{}:{}".format(id, "ping", field_data))
                self.show_image(number, video_name, "{}_{}_{}.jpg".format("ping", video_name, number), "#")
                try:
                    ping = int(input("请确认ping值:"))
                    self.cursor.execute("update {} set ping={} where id={};".format(self.table_name, ping, id))
                except:
                    pass
 
    def remove_path(self, path):
        try:
            # os.remove(path)
            pass
        except:
            pass
 
    def close_sql(self):
        self.cursor.close()
        self.db.close()
 
 
if __name__ == '__main__':
    pfs = GetFps()
    pfs.get_image()
    # pfs.revised_data()

 

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门