Files
2025-03-26 21:57:50 +01:00

71 lines
2.6 KiB
Python

import os
import server
from aiohttp import web
from PIL import Image
import io
from pyzbar.pyzbar import decode
APP_NAME = "Avatar Generation Experience"
APP_DESCRIPTION = "Experience for Avatar Generation with multiple options."
APP_VERSION = "0.0.1"
APP_FOLDER_NAME = "baseapp"
NODE_CLASS_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS = {}
WEBROOT = os.path.join(os.path.dirname(os.path.realpath(__file__)), "web")
@server.PromptServer.instance.routes.get("/" + APP_FOLDER_NAME)
def deungeon_entrance(request):
return web.FileResponse(os.path.join(WEBROOT, "index.html"))
server.PromptServer.instance.routes.static("/" + APP_FOLDER_NAME + "/js/", path=os.path.join(WEBROOT, "js"))
server.PromptServer.instance.routes.static("/" + APP_FOLDER_NAME + "/css/", path=os.path.join(WEBROOT, "css"))
server.PromptServer.instance.routes.static("/" + APP_FOLDER_NAME + "/assets/", path=os.path.join(WEBROOT, "assets"))
server.PromptServer.instance.routes.static("/" + APP_FOLDER_NAME + "/workflows/", path=os.path.join(WEBROOT, "workflows"))
@server.PromptServer.instance.routes.post("/" + APP_FOLDER_NAME + "/scan_qrcode")
async def scan_qrcode(request):
print ("Scanning QR code...")
try:
# Check if the request is multipart/form-data
if request.content_type.startswith("multipart/form-data"):
# Read multipart data
reader = await request.multipart()
field = await reader.next()
# Read file content
file_data = await field.read()
else:
return web.json_response({"status": "error", "message": "Request format not supported"})
# Load image from bytes using PIL
try:
image = Image.open(io.BytesIO(file_data))
except Exception as e:
return web.json_response({"status": "error", "message": f"Error opening image: {str(e)}"})
# Decode QR code
decoded_objects = decode(image)
# If no QR code is found, return None
if not decoded_objects:
print("No QR code found in the image.")
return web.json_response({"status": "success", "decoded_text": ""})
# Extract decoded text from the first QR code found
decoded_text = decoded_objects[0].data.decode("utf-8")
print(f"Decoded QR code: {decoded_text}")
code, name = decoded_text.split("__")
# Send decoded text via WebSocket
server.PromptServer.instance.send_sync("scan-result", {"text": code, "name": name})
return web.json_response({"status": "success", "decoded_text": decoded_text})
except Exception as e:
return web.json_response({"status": "error", "message": str(e)})