For a certain application, I want to have 1 main script run 2 scripts: one that uses mediapipe and another one that opens an HTML file through a Python local server. However, when linking the file importing mediapipe, it for whatever reason throws the error: "[WinError 6] The handle is invalid"
Code running in Python 3.9 Anaconda environment with Mediapipe version: mediapipe==0.10.11 (Mediapipe is causing trouble in later Python versions)
> Opening browser...
Starting httpd server on port 8000
Failed to start server: [WinError 6] Controlador no válido
If the mediapipe import is commented, it works perfectly as intended.
Project structure:
./main.py
./server/make_a_server.py
./mp/gesture_detection.py
./server/anhtml.html
main.py
from server.make_a_server import open_browser
from mp.gesture_detection import detect
def main():
open_browser(8000)
detect()
if __name__ == '__main__':
main()
make_a_server.py
import os
import threading
import keyboard
import webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler
def get_full_path(relative_path):
current_dir = os.path.dirname(os.path.abspath(__file__))
return os.path.join(current_dir, relative_path)
def start_server(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler, port=8000):
try:
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f"Starting httpd server on port {port}")
httpd.serve_forever()
except Exception as e:
print(f"Failed to start server: {e}")
def open_browser(port):
print("> Opening browser...")
relative_path = 'anhtml.html'
full_path = get_full_path(relative_path)
directory = os.path.dirname(full_path)
server_thread = threading.Thread(target=start_server, args=(HTTPServer, lambda *args, directory='.', **kwargs: SimpleHTTPRequestHandler(*args, directory=directory, **kwargs), port,))
server_thread.daemon = True
server_thread.start()
url = f"http://localhost:{port}/{os.path.basename(relative_path)}"
webbrowser.open_new(url)
keyboard.wait('q')
gesture_detection.py
import mediapipe as mp
def detect():
pass
Project idea is to send gesture data from mediapipe to the website and move a folium map, but for the sake of getting to the root cause, I stripped as much irrelevant code while keeping the same error behavior. Both folders have init.py to properly link between folders.
Any idea what could be happening? Is this an issue from my side?
Made sure that I was using the latest Mediapipe version, uninstalling it and reinstalling. Creating a new environment and only installing Mediapipe.
I even tried making the Python local server not start through threading, just in case that was an issue. Didn't work either.