File size: 2,799 Bytes
d785ac4
ed139ff
d785ac4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed139ff
 
d785ac4
ed139ff
d785ac4
7846b09
e55dc1c
7846b09
d785ac4
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import cv2
import os
import numpy as np
from pyzbar.pyzbar import decode
from fastapi import FastAPI, File, UploadFile
from typing import List
import shutil
from logging import getLogger, StreamHandler, DEBUG
logger = getLogger(__name__)
handler = StreamHandler(); handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)
logger.propagate = False

    
def qrdec_zbar(image_path):
    # load image
    image = cv2.imread(image_path)

    # QRコードをデコードする
    dec_infos = decode(image)

    result_data = ""
    readable = False
    if dec_infos:
        for dec_info in dec_infos:
            if not dec_info =="":
                result_data = dec_info.data.decode("utf-8")
                readable = True
                break
    else:
        pass

    return {"data": result_data, "readable": readable}


# -----------------------------------------------------------
# initial
# -----------------------------------------------------------
font = cv2.FONT_HERSHEY_SIMPLEX


# -----------------------------------------------------------
# function_qr_dec
# -----------------------------------------------------------
def qrdec_cv2(img_bgr):

    # QRCodeDetectorインスタンス生成
    qrd = cv2.QRCodeDetector()

    # QRコードデコード
    retval, dec_infos, _, _ = qrd.detectAndDecodeMulti(img_bgr)

    result_data = ""
    readable = False

    if retval:
        for dec_info in dec_infos:
            if not dec_info=="":
                result_data = dec_info
                readable = True
                break
    else:
        pass
    return {"data": result_data, "readable": readable}






# -----------------------------------------------------------
# sample program
# -----------------------------------------------------------
def qrdec(file_path):
    img_BGR = cv2.imread(file_path, cv2.IMREAD_COLOR)
    cv2_result = qrdec_cv2(img_BGR)
    zbar_result = qrdec_zbar(file_path)
        
    is_safe_to_read = cv2_result["readable"] and zbar_result["readable"]
    readable = cv2_result["readable"] or zbar_result["readable"]

    decoded = ""
    if cv2_result["readable"]:
        decoded = cv2_result["data"]
    elif zbar_result["readable"]:
        decoded = zbar_result["data"]

    return {"is_safe_to_read": is_safe_to_read, "readable": readable, "decoded": decoded}


app = FastAPI()


@app.post("/")
async def upload_image(image: UploadFile = File(...)):
    temp_file_path = os.path.join('/tmp', image.filename)

    # 画像ファイルの内容を保存または処理する
    with open(temp_file_path, "wb") as buffer:
        shutil.copyfileobj(image.file, buffer)
    # logger.info(f"target file : {temp_file_path}")
    result = qrdec(temp_file_path)
    # logger.info(f"response : {result}")
    return result