CV app docker fix

This commit is contained in:
Luciano Gervasoni
2025-04-30 22:31:24 +02:00
parent d7df5b4ea4
commit aa7aca3e66
3 changed files with 28 additions and 31 deletions

View File

@@ -18,11 +18,10 @@ RUN curl "https://drive.usercontent.google.com/download?id=1lHTrW1rmYoYnMSUlhLwq
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
RUN pip freeze
# CMD ["gunicorn", "--bind", "0.0.0.0:5000", "--workers", "2", "app:app"]
CMD ["uvicorn", "--port", "5000", "--workers", "2", "app:app"]
CMD ["uvicorn", "--host", "0.0.0.0", "--port", "5000", "--workers", "1", "--log-level", "info", "app:app"]
# docker build -t fetcher_cv .
# docker run --rm -p 5000:5000 fetcher_cv

View File

@@ -6,7 +6,7 @@ import numpy as np
import cv2
import traceback
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[logging.StreamHandler()])
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[logging.StreamHandler()])
from cv_processor import process
from pydantic import BaseModel
@@ -16,6 +16,30 @@ class Item(BaseModel):
app = FastAPI()
# Define the NiceGUI UI components
@ui.page("/")
def main_page():
async def handle_upload(e: events.UploadEventArguments) -> None:
ui.notify('Processing...')
# Read content -> image
nparr = np.frombuffer(e.content.read(), np.uint8)
img_np_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Async process
results = await run.io_bound(process, img_np_bgr)
# Display
with ui.dialog() as dialog:
# Encode
retval, buffer = cv2.imencode('.png', results.get("image"))
img_buffer_encoded = base64.b64encode(buffer).decode('utf-8')
img_encoded = "data:image/png;base64,{}".format(img_buffer_encoded)
content = ui.image(img_encoded).props('fit=scale-down')
dialog.open()
ui.upload(on_upload=handle_upload, auto_upload=True, on_rejected=lambda: ui.notify('Rejected!')).props('accept=image').classes('max-w-full')
ui.run_with(app, title="CV")
@app.post('/process')
def process_image(item: Item):
logging.info("POST /process")
@@ -50,29 +74,3 @@ def process_image(item: Item):
except Exception as e:
logging.warning("Exception: {}".format(traceback.format_exc()))
return {"error": traceback.format_exc()}
# Define the NiceGUI UI components
@ui.page("/")
def main_page():
async def handle_upload(e: events.UploadEventArguments) -> None:
ui.notify('Processing...')
# Read content -> image
nparr = np.frombuffer(e.content.read(), np.uint8)
img_np_bgr = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# Async process
results = await run.io_bound(process, img_np_bgr)
# Display
with ui.dialog() as dialog:
# Encode
retval, buffer = cv2.imencode('.png', results.get("image"))
img_buffer_encoded = base64.b64encode(buffer).decode('utf-8')
img_encoded = "data:image/png;base64,{}".format(img_buffer_encoded)
content = ui.image(img_encoded).props('fit=scale-down')
dialog.open()
ui.upload(on_upload=handle_upload, auto_upload=True, on_rejected=lambda: ui.notify('Rejected!')).props('accept=image').classes('max-w-full')
if __name__ == '__main__':
ui.run_with(app, title="CV")

View File

@@ -1,7 +1,7 @@
import cv2
import numpy as np
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[logging.StreamHandler()])
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s', handlers=[logging.StreamHandler()])
# Age
from mivolo.predictor import Predictor