Using FastAPI framework, I wrote a program that takes live video feed from webcam and shows it in the front end using image_feed
function. Each frame is processed with a function namely process_image(img, scale=0.35)
. From that function, it updates a global dataframe df
.
When this process is complete, the live feed is stopped by making state.live = False
. The function then calls /upload_df
, that should convert the df
into a HTML table and show it in the HTML front end. (With a print command, I checked the html table conversion is successful).
However, the program does not display the df
in the front end. Instead, it shows the following error message-
“INFO: 127.0.0.1:2948 – “POST /upload_df HTTP/1.1″ 200 OK
Error sending POST request to /upload_df: Expecting value: line 1 column 1 (char 0)”
What is going on and how to fix this? I am using pycharm in windows.
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
html_file = "upload11.html"
class FeedState:
live = True # If True, stream live video; if False, show last processed frame
last_processed_frame = None # Store the last processed image
lock = Lock() # To ensure thread safety when accessing the state
state = FeedState()
df = pd.DataFrame(columns=["A", "B", "TF n(+ve)","TF n(-ve)","QA n(+ve)","QA n(-ve)"])
def process_image(img, scale=0.35):
global df
#<Code that process the img, and updates df>
with state.lock:
state.live = False
try:
print("Video feed stopped. Now calling upload_df function")
response = requests.post("http://127.0.0.1:8000/upload_df")
print("Request to /upload_df:", response.json())
except Exception as e:
print("Error sending POST request to /upload_df:", e)
return <processed_image>
@app.post("/upload_df", response_class=HTMLResponse)
async def upload_df(request: Request):
global df
# Convert DataFrame to HTML with styling
df_html = df.to_html(classes="table table-bordered", escape=False)
return templates.TemplateResponse(html_file, {"request": request, "df_html": df_html})
@app.get("/image_feed")
def image_feed():
def generate_images():
cap = cv2.VideoCapture(0) # Open the default webcam
if not cap.isOpened():
raise RuntimeError("Could not access the webcam.")
ret, last_img = cap.read()
while True:
if state.live:
ret, img = cap.read()
last_img=process_image(img)
_, buffer = cv2.imencode(".jpg", last_img)
frame_bytes = buffer.tobytes()
yield (
b"--framern"
b"Content-Type: image/jpegrnrn" + frame_bytes + b"rn")
return StreamingResponse(generate_images(), media_type="multipart/x-mixed-replace; boundary=frame")
@app.get("/", response_class=HTMLResponse)
async def read_root():
return templates.TemplateResponse(html_file, {"request": {}})
def open_browser():
webbrowser.open("http://127.0.0.1:8000")
if __name__ == "__main__":
# Run the browser-opening function in a separate thread
threading.Thread(target=open_browser).start()
# Start the Uvicorn server
uvicorn.run("webInterface11:app", host="127.0.0.1", port=8000, reload=True)
3