Spaces:
Runtime error

File size: 2,271 Bytes
f6d42dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d46c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6d42dc
87d46c7
f6d42dc
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
File Name:     main
Description :
date:          2024/1/18 018
"""
import os
import time

import cv2
import numpy as np
import gradio as gr
from PIL import Image
from detection.dete import Det
from recognize.test_code import infer


def cv2array(image):
    return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))


def greet(image):
    # 类型转换
    array = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    start_time = time.time()
    print('>>>检测开始>>>')
    try:
        det = Det(array)
        end_time = time.time()
        timec = end_time - start_time
        img = np.ones((200, 800, 3), dtype=np.uint8)
        img *= 255  # white background
        ocr_text = ""
        if det.lps:
            for x, _ in enumerate(det.lps):
                # cv2.imshow(str(x), _)
                if x == 0:
                    model_path = r'recognize/model/LPDR_ZH.pkl'
                else:
                    model_path = r'recognize/model/LPDR_0_Z.pkl'
                ocr_text += infer(det.lps[x], model_path=model_path)
                height = int(_.shape[0])
                width = int(_.shape[1])
                for i in range(height):
                    for j in range(width):
                        img[i + 40, j + 40 + (65 * x)] = _[i, j]
            print(f"车牌号:{ocr_text}")
            print(f"耗时:{timec}")
            print("<<<检测结束<<<")
            # cv2.waitKey(0)
            return cv2array(det.lp_img), cv2array(img), ocr_text
        else:
            print(f"耗时:{timec}")
            print("<<<检测结束<<<")
            return cv2array(det.lp_img), None, None
    except:
        print("<<<检测结束<<<")
        return None, None, None


demo = gr.Interface(
    fn=greet,
    # 自定义输入框
    # 具体设置方法查看官方文档
    inputs=gr.Image(sources=["upload", "clipboard"], label="输入正面清晰的车牌", height="60vh"),
    outputs=[gr.Image(sources=[], label="检测", height="20vh"),
             gr.Image(sources=[], label="分割", height="20vh"),
             gr.Label(label="识别", )
             ],
    examples=[[fr'static/exa_img/{_}'] for _ in os.listdir(r'static/exa_img')]
)
demo.launch(server_name='0.0.0.0', server_port=7860)