Spaces:
Sleeping
Sleeping
initialize
Browse files- .gitignore +1 -0
- Dockerfile +11 -0
- main.py +101 -0
- requirements.txt +21 -0
- test.py +7 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
./data/
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from pyzbar.pyzbar import decode
|
| 4 |
+
from fastapi import FastAPI, File, UploadFile
|
| 5 |
+
from typing import List
|
| 6 |
+
import shutil
|
| 7 |
+
from logging import getLogger, StreamHandler, DEBUG
|
| 8 |
+
logger = getLogger(__name__)
|
| 9 |
+
handler = StreamHandler(); handler.setLevel(DEBUG)
|
| 10 |
+
logger.setLevel(DEBUG)
|
| 11 |
+
logger.addHandler(handler)
|
| 12 |
+
logger.propagate = False
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def qrdec_zbar(image_path):
|
| 16 |
+
# load image
|
| 17 |
+
image = cv2.imread(image_path)
|
| 18 |
+
|
| 19 |
+
# QRコードをデコードする
|
| 20 |
+
dec_infos = decode(image)
|
| 21 |
+
|
| 22 |
+
result_data = ""
|
| 23 |
+
readable = False
|
| 24 |
+
if dec_infos:
|
| 25 |
+
for dec_info in dec_infos:
|
| 26 |
+
if not dec_info =="":
|
| 27 |
+
result_data = dec_info.data.decode("utf-8")
|
| 28 |
+
readable = True
|
| 29 |
+
break
|
| 30 |
+
else:
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
return {"data": result_data, "readable": readable}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# -----------------------------------------------------------
|
| 37 |
+
# initial
|
| 38 |
+
# -----------------------------------------------------------
|
| 39 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# -----------------------------------------------------------
|
| 43 |
+
# function_qr_dec
|
| 44 |
+
# -----------------------------------------------------------
|
| 45 |
+
def qrdec_cv2(img_bgr):
|
| 46 |
+
|
| 47 |
+
# QRCodeDetectorインスタンス生成
|
| 48 |
+
qrd = cv2.QRCodeDetector()
|
| 49 |
+
|
| 50 |
+
# QRコードデコード
|
| 51 |
+
retval, dec_infos, _, _ = qrd.detectAndDecodeMulti(img_bgr)
|
| 52 |
+
|
| 53 |
+
result_data = ""
|
| 54 |
+
readable = False
|
| 55 |
+
|
| 56 |
+
if retval:
|
| 57 |
+
for dec_info in dec_infos:
|
| 58 |
+
if not dec_info=="":
|
| 59 |
+
result_data = dec_info
|
| 60 |
+
readable = True
|
| 61 |
+
break
|
| 62 |
+
else:
|
| 63 |
+
pass
|
| 64 |
+
return {"data": result_data, "readable": readable}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# -----------------------------------------------------------
|
| 72 |
+
# sample program
|
| 73 |
+
# -----------------------------------------------------------
|
| 74 |
+
def qrdec(file_path):
|
| 75 |
+
img_BGR = cv2.imread(file_path, cv2.IMREAD_COLOR)
|
| 76 |
+
cv2_result = qrdec_cv2(img_BGR)
|
| 77 |
+
zbar_result = qrdec_zbar(file_path)
|
| 78 |
+
|
| 79 |
+
is_safe_to_read = cv2_result["readable"] and zbar_result["readable"]
|
| 80 |
+
readable = cv2_result["readable"] or zbar_result["readable"]
|
| 81 |
+
|
| 82 |
+
decoded = ""
|
| 83 |
+
if cv2_result["readable"]:
|
| 84 |
+
decoded = cv2_result["data"]
|
| 85 |
+
elif zbar_result["readable"]:
|
| 86 |
+
decoded = zbar_result["data"]
|
| 87 |
+
|
| 88 |
+
return {"is_safe_to_read": is_safe_to_read, "readable": readable, "decoded": decoded}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
app = FastAPI()
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
@app.post("/")
|
| 95 |
+
async def upload_image(image: UploadFile = File(...)):
|
| 96 |
+
# 画像ファイルの内容を保存または処理する
|
| 97 |
+
with open(image.filename, "wb") as buffer:
|
| 98 |
+
shutil.copyfileobj(image.file, buffer)
|
| 99 |
+
result = qrdec(image.filename)
|
| 100 |
+
logger.info(f"response : {result}")
|
| 101 |
+
return result
|
requirements.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
annotated-types==0.6.0
|
| 2 |
+
anyio==3.7.1
|
| 3 |
+
certifi==2023.11.17
|
| 4 |
+
charset-normalizer==3.3.2
|
| 5 |
+
click==8.1.7
|
| 6 |
+
exceptiongroup==1.2.0
|
| 7 |
+
fastapi==0.104.1
|
| 8 |
+
h11==0.14.0
|
| 9 |
+
idna==3.6
|
| 10 |
+
numpy==1.26.2
|
| 11 |
+
opencv-python-headless==4.8.1.78
|
| 12 |
+
pydantic==2.5.2
|
| 13 |
+
pydantic_core==2.14.5
|
| 14 |
+
python-multipart==0.0.6
|
| 15 |
+
pyzbar==0.1.9
|
| 16 |
+
requests==2.31.0
|
| 17 |
+
sniffio==1.3.0
|
| 18 |
+
starlette==0.27.0
|
| 19 |
+
typing_extensions==4.9.0
|
| 20 |
+
urllib3==2.1.0
|
| 21 |
+
uvicorn==0.24.0.post1
|
test.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
url = 'http://127.0.0.1:8000/'
|
| 4 |
+
files = {'image': open('./data/catch_copy_.png', 'rb')}
|
| 5 |
+
|
| 6 |
+
response = requests.post(url, files=files)
|
| 7 |
+
print(response.text)
|