Код: Выделить всё
from flask import Flask, Response, render_template
import cv2
import signal
import sys
import os
app = Flask(__name__)
# Initialize the camera
camera = cv2.VideoCapture(0)
def generate_frames():
while True:
success, frame = camera.read() # Capture frame-by-frame
if not success:
break
else:
_, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route('/')
def index():
return render_template('index.html') # Serve the HTML template
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(),
mimetype='multipart/x-mixed-replace; boundary=frame')
def cleanup(*args):
"""Release the camera resource when the app stops."""
print("Releasing camera...")
camera.release()
sys.exit(0)
# Handle signals for a clean exit
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGTERM, cleanup)
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0") # Allow connections from all IPs
Код: Выделить всё
# Use a lightweight Python image
FROM python:3.10
# Set the working directory
WORKDIR /flaskProject
# Install required system libraries
RUN apt-get update && \
apt-get install -y libgl1-mesa-glx libglib2.0-0 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install Python dependencies
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code into the container
COPY . .
# Expose the application port
EXPOSE 5005
# Run the Flask application
CMD ["python", "app.py"]
Подробнее здесь: https://stackoverflow.com/questions/792 ... dockerfile
Мобильная версия