can you make it professional with beatiful style "<!DOCTYPE html> <html lang="en">…
- User
can you make it professional with beatiful style "
<!-- Video Upload Form --> <h2>Upload Video</h2> <form action="/upload/video" method="post" enctype="multipart/form-data"> <input type="file" name="video" accept="video/*"> <button type="submit">Upload</button> </form> - Assistant
Certainly! Below is a more professional template with a simple, yet beautiful style using CSS to enhance the appearance of your HTML form:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Home</title> <style> body { font-family: 'Arial', sans-serif; background-color: #f4f4f4; margin: 0; padding: 20px; display: flex; justify-content: center; align-items: center; min-height: 100vh; } .container { background: #fff; border-radius: 5px; padding: 20px; box-shadow: 0 3px 6px rgba(0,0,0,0.1); max-width: 400px; } h2 { margin-top: 0; color: #333; } .upload-btn-wrapper { position: relative; overflow: hidden; display: inline-block; } .btn { border: 2px solid #008CBA; color: white; background-color: #008CBA; padding: 8px 20px; border-radius: 8px; font-size: 15px; font-weight: bold; } .upload-btn-wrapper input[type=file] { font-size: 100px; position: absolute; left: 0; top: 0; opacity: 0; } button[type=submit] { background-color: #4CAF50; color: white; border: none; padding: 8px 20px; border-radius: 8px; font-size: 15px; font-weight: bold; margin-top: 10px; cursor: pointer; } button[type=submit]:hover { background-color: #45a049; } </style> </head> <body> <div class="container"> <!-- Video Upload Form --> <h2>Upload Video</h2> <form action="/upload/video" method="post" enctype="multipart/form-data"> <div class="upload-btn-wrapper"> <button class="btn">Choose a video</button> <input type="file" name="video" accept="video/*"> </div> <button type="submit">Upload Video</button> </form> </div> </body> </html>This improved HTML document includes style definitions in the
<head>using the<style>tags to create a more polished and visually enjoyable form. We've added box shadows, button styling, and a card-like container for the form, providing a modern and minimalist aesthetic. - User
make this form look god " Choose a video No file chosen Upload Video
- Assistant
To make your form look good, you can apply some custom CSS to style the buttons, the file upload input, and the layout. Below is an example of how you could enhance the visual appearance of your form:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Upload Video</title> <style> body { font-family: 'Arial', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f3f3f3; } .upload-form { background: #ffffff; padding: 20px; border-radius: 10px; box-shadow: 0 5px 15px rgba(0,0,0,0.15); } .upload-btn-wrapper { margin-bottom: 10px; position: relative; overflow: hidden; } .btn { border: 2px solid #007BFF; color: white; background-color: #007BFF; padding: 10px 20px; border-radius: 5px; font-size: 16px; cursor: pointer; } .upload-btn-wrapper input[type=file] { font-size: 100px; position: absolute; left: 0; top: 0; opacity: 0; } .file-label { display: block; margin-bottom: 20px; color: #666666; font-size: 14px; } .submit-btn { display: inline-block; padding: 10px 20px; background-color: #28a745; border: none; border-radius: 5px; color: white; cursor: pointer; font-size: 16px; } .submit-btn:hover { background-color: #218838; } </style> </head> <body> <form action="/upload/video" method="post" enctype="multipart/form-data" class="upload-form"> <div class="upload-btn-wrapper"> <button class="btn" type="button">Choose a video</button> <input type="file" name="video" id="video-upload" accept="video/*" onchange="updateLabel()"> </div> <label for="video-upload" class="file-label" id="file-label">No file chosen</label> <button type="submit" class="submit-btn">Upload Video</button> </form> <script> function updateLabel() { var upload = document.getElementById('video-upload'); var label = document.getElementById('file-label'); label.innerHTML = upload.files[0].name; } </script> </body> </html>Here’s an explanation of the enhancements made:
- The body of the page is set up as a flex container to center the form both vertically and horizontally.
- The form itself has a white background, padding, a slight border-radius, and a shadow for a subtle 3D effect. This creates the look of a card on the page.
- The styling of the file input button and submit button is consistent, with both matching in size and border-radius.
- The file label updates to show the name of the chosen file using a simple JavaScript function attached to the file input's
onchangeevent. - To improve accessibility, confirm that the custom styled button triggers the file input by setting its
typeattribute tobuttonand using JavaScript for the label update.
- User
File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\applications.py", line 124, in call await self.middleware_stack(scope, receive, send) File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 184, in call raise exc File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\middleware\errors.py", line 162, in call await self.app(scope, receive, send) File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\middleware\exceptions.py", line 79, in call raise exc File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\middleware\exceptions.py", line 68, in call await self.app(scope, receive, sender) File "c:\Users\ayelg\anaconda3\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in call raise e File "c:\Users\ayelg\anaconda3\Lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in call await self.app(scope, receive, send) File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\routing.py", line 706, in call await route.handle(scope, receive, send) File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\routing.py", line 276, in handle await self.app(scope, receive, send) File "c:\Users\ayelg\anaconda3\Lib\site-packages\starlette\routing.py", line 66, in app response = await func(request) ^^^^^^^^^^^^^^^^^^^ File "c:\Users\ayelg\anaconda3\Lib\site-packages\fastapi\routing.py", line 237, in app raw_response = await run_endpoint_function( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\ayelg\anaconda3\Lib\site-packages\fastapi\routing.py", line 163, in run_endpoint_function return await dependant.call(**values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ayelg\OneDrive\Documents\GitHub\yolo_detection_api\main.py", line 112, in upload_video return json.dumps(processed_results) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\ayelg\anaconda3\Lib\json_init.py", line 231, in dumps return _default_encoder.encode(obj) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\ayelg\anaconda3\Lib\json\encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\ayelg\anaconda3\Lib\json\encoder.py", line 258, in iterencode return _iterencode(o, 0) ^^^^^^^^^^^^^^^^^ File "c:\Users\ayelg\anaconda3\Lib\json\encoder.py", line 180, in default raise TypeError(f'Object of type {o.class.name} ' TypeError: Object of type ndarray is not JSON serializable
- Assistant
The error message you're encountering indicates that within your FastAPI application you're trying to convert a Python object to JSON using
json.dumps, but the object contains a NumPyndarray, which is not natively serializable to JSON.To resolve this issue, you need to convert your
ndarrayto a serializable list before attempting to JSON-serialize the entire object. Here's how you can modify yourupload_videofunction to handle this:import json import numpy as np from fastapi import FastAPI, UploadFile, File from typing import List app = FastAPI() # ... your other endpoint definitions ... @app.post("/upload/video") async def upload_video(video: UploadFile = File(...)): # your processing logic here, resulting in an ndarray processed_results = your_video_processing_function(video) # This function returns a ndarray (for example) # Before returning, convert ndarray to list if it's not already in an acceptable format if isinstance(processed_results, np.ndarray): processed_results_list = processed_results.tolist() # Convert ndarray to list return json.dumps(processed_results_list) # Now you can safely serialize to JSON else: # Handle other data types appropriately, might require custom serialization return json.dumps(processed_results) # Assuming 'processed_results' is JSON serializable # Further logic, if necessary...The
ndarray.tolist()method converts the array into a nested list whichjson.dumpscan then serialize. Depending on the structure and types within your NumPy array, you may need to handle additional serialization for objects such as datetimes or custom classes.Remember to ensure you adjust the implementation to fit with the specifics of your application and processing function.
- User
the upload image in ec2 with ngix and uvicorn not return the post " @app.post("/upload/video") async def upload_video(video: UploadFile = File(...)): # Ensure the directory exists, create it if it doesn't video_directory.mkdir(parents=True, exist_ok=True)
# Define the path where the video will be saved video_path = video_directory / video.filename # Write the video data to the specified path with open(video_path, "wb") as buffer: buffer.write(await video.read()) # Perform object tracking on the uploaded video results = onnx_model(video_path, save=True) # Process tracking results processed_results = [] for result in results: # Check if track method outputs are available and then process each detection if hasattr(result, 'boxes') and len(result.boxes): tracked_detections = result.boxes # Get the detections with tracking for det in tracked_detections: cls_id = int(det.cls) # Extract class ID conf = float(det.conf) # Extract confidence score # track_id = int(det.id) # Extract track ID xyxy = det.xyxy[0].tolist() # Extract bounding box coordinates # Placeholder depth estimation using basic geometric principles # Here, we assume the depth is inversely proportional to the area of the bounding box # You can replace this with a more accurate depth estimation method if available width = xyxy[2] - xyxy[0] height = xyxy[3] - xyxy[1] depth = 1000 / (width * height) # Placeholder depth calculation # Get the class name from model.names class_name = result.names[cls_id] # Add extracted information to the processed results processed_results.append({ "class_id": cls_id, "confidence": conf, # "track_id": track_id, "bounding_box": xyxy }) # Return the tracking results as JSON return {"message": "Video uploaded successfully"} - Assistant
If you have set up NGINX as a reverse proxy for Uvicorn and your POST request is not returning a response (such as when you are uploading a video), there may be several factors to consider. Here is an example configuration snippet for NGINX that might be relevant to your setup:
server { listen 80; server_name your_ec2_domain.com; location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # We increase the maximum file size that can be uploaded client_max_body_size 100M; } }In this example,
client_max_body_sizeis increased to allow for large file uploads (in this case, up to 100M).After you've checked your NGINX configuration, make sure your
upload_videoendpoint does the following:- Saves the uploaded file correctly.
- Processes the file without errors.
- Returns a suitable response.
Here's an updated version of your FastAPI endpoint ensuring that the file is saved and processed but simplifying the example for demonstration purposes:
from fastapi import FastAPI, File, UploadFile from pathlib import Path app = FastAPI() video_directory = Path("/path/to/video/directory") @app.post("/upload/video") async def upload_video(video: UploadFile = File(...)): # Ensure the directory exists, create it if it doesn't video_directory.mkdir(parents=True, exist_ok=True) # Define the path where the video will be saved video_path = video_directory / video.filename # Write the video data to the specified path async with open(video_path, "wb") as buffer: data = await video.read() # Read the data from the uploaded file buffer.write(data) # Placeholder for calling the model processing # results = onnx_model(video_path, save=True) # Process tracking results processed_results = [] # ... (assuming the processing is done and results are stored) return {"message": "Video uploaded successfully"} # ... other parts of your FastAPI application ...Make sure to replace
"/path/to/video/directory"with the actual path where you want to save uploaded videos. Also, the asynchronous file writing method is used here (i.e.,async with open(...)).If you continue experiencing issues, it will be beneficial to inspect the logs for both Uvicorn and NGINX.
- For Uvicorn, you may increase the log level to see more verbose output:
uvicorn main:app --host 0.0.0.0 --port 8000 --log-level debug - For NGINX, review the error log specified in your NGINX configuration or by default located in
/var/log/nginx/error.log.
Check that you've correctly configured the proxy settings and that NGINX has the permissions to access the Uvicorn socket or network port, and ensure your firewall rules allow HTTP traffic.
Finally, remember that if you are using form data to submit your video file, you might need to adjust the frontend and backend to correctly handle multi-part form data.
If these troubleshooting steps do not resolve your issue, please provide more context or any errors you see in your logs for further assistance.