diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..acd5a2e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +waveshare_OLED/__pycache__/ \ No newline at end of file diff --git a/camera_opencv.py b/camera_opencv.py index 8cf2d58..0cd7c48 100755 --- a/camera_opencv.py +++ b/camera_opencv.py @@ -425,24 +425,20 @@ def frames(): cvt.start() while True: - # read current frame - _, img = camera.read() + success, img = camera.read() # Capture frame-by-frame + if not success or img is None: + # If the frame was not captured successfully, skip encoding + print("Failed to capture image. Retrying...") + time.sleep(0.1) # Add a short delay before trying again + continue - if Camera.modeSelect == 'none': - cvt.pause() - else: - if cvt.CVThreading: - pass - else: - cvt.mode(Camera.modeSelect, img) - cvt.resume() - try: - img = cvt.elementDraw(img) - except: - pass - - # encode as a jpeg image and return it - yield cv2.imencode('.jpg', img)[1].tobytes() + # Proceed with encoding only if the image is valid + try: + encoded_img = cv2.imencode('.jpg', img)[1].tobytes() + yield encoded_img + except cv2.error as e: + print(f"Error encoding image: {e}") + continue class Functions(threading.Thread): diff --git a/setup.py b/setup.py index a80e672..8235d8b 100755 --- a/setup.py +++ b/setup.py @@ -3,138 +3,87 @@ # Date : 2020/11/24 import os -import time -import re -curpath = os.path.realpath(__file__) -thisPath = os.path.dirname(curpath) - -def replace_num(file,initial,new_num): - newline="" - str_num=str(new_num) - with open(file,"r") as f: +def replace_num(file, initial, new_num): + """Replace a specific line in a file with a new line.""" + newline = "" + with open(file, "r") as f: for line in f.readlines(): - if(line.find(initial) == 0): - line = (str_num+'\n') + if line.startswith(initial): + line = f"{new_num}\n" newline += line - with open(file,"w") as f: - f.writelines(newline) - -for x in range(1,4): - if os.system("sudo apt update") == 0: - break - -for x in range(1,4): - if os.system("sudo apt -y dist-upgrade") == 0: - break - -for x in range(1,4): - if os.system("sudo apt clean") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install -U pip") == 0: - break - -for x in range(1,4): - if os.system("sudo apt-get install -y python-dev python3-pip libfreetype6-dev libjpeg-dev build-essential") == 0: - break - -for x in range(1,4): - if os.system("sudo -H pip3 install --upgrade luma.oled") == 0: - break - -for x in range(1,4): - if os.system("sudo apt-get install -y i2c-tools") == 0: - break - -for x in range(1,4): - if os.system("sudo apt-get install -y python3-smbus") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install icm20948") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install flask") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install flask_cors") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install websockets") == 0: - break + with open(file, "w") as f: + f.write(newline) -try: - replace_num("/boot/config.txt",'#dtparam=i2c_arm=on','dtparam=i2c_arm=on') -except: - print('try again') +def run_command(command, max_retries=3): + """Run a shell command with retry mechanism.""" + for _ in range(max_retries): + if os.system(command) == 0: + return True + return False -try: - replace_num("/boot/config.txt",'[all]','[all]\ngpu_mem=128') -except: - print('try again') - -try: - replace_num("/boot/config.txt",'camera_auto_detect=1','#camera_auto_detect=1\nstart_x=1') -except: - print('try again') - -try: - replace_num("/boot/config.txt",'camera_auto_detect=1','#camera_auto_detect=1') -except: - print('try again') - - -for x in range(1,4): - if os.system("sudo pip3 install opencv-contrib-python==3.4.11.45") == 0: - break - elif os.system("sudo pip3 install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple opencv-contrib-python==3.4.11.45") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 uninstall -y numpy") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install numpy==1.21") == 0: - break - elif os.system("sudo pip3 install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple numpy==1.21") == 0: - break - -for x in range(1,4): - if os.system("sudo apt-get -y install libhdf5-dev libhdf5-serial-dev libatlas-base-dev libjasper-dev") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install imutils zmq pybase64 psutil") == 0: - break - elif os.system("sudo pip3 install -i http://pypi.douban.com/simple/ --trusted-host=pypi.douban.com/simple imutils zmq pybase64 psutil") == 0: - break - -for x in range(1,4): - if os.system("sudo apt-get install -y util-linux procps hostapd iproute2 iw haveged dnsmasq") == 0: - break - -for x in range(1,4): - if os.system("sudo pip3 install pi-ina219") == 0: - break - - -for x in range(1,4): - if os.system("cd " + thisPath + " && cd .. && sudo git clone https://github.com/oblique/create_ap") == 0: - break - -try: - os.system("cd " + thisPath + " && cd .. && cd create_ap && sudo make install") -except: - pass +# Paths +curpath = os.path.realpath(__file__) +thisPath = os.path.dirname(curpath) -replace_num('/etc/rc.local','exit 0','cd '+thisPath+' && sudo python3 webServer.py &\nexit 0') +# Update system and install packages +commands = [ + "sudo apt update", + "sudo apt -y dist-upgrade", + "sudo apt clean", + "sudo pip3 install --break-system-packages -U pip", + "sudo apt-get install -y python-dev python3-pip libfreetype6-dev libjpeg-dev build-essential", + "sudo -H pip3 install --break-system-packages --upgrade luma.oled", + "sudo apt-get install -y i2c-tools", + "sudo apt-get install -y python3-smbus", + "sudo pip3 install --break-system-packages icm20948", + "sudo pip3 install --break-system-packages flask", + "sudo pip3 install --break-system-packages flask_cors", + "sudo pip3 install --break-system-packages websockets", +] + +for cmd in commands: + run_command(cmd) + +# Modify /boot/config.txt +config_modifications = [ + ('/boot/config.txt', '#dtparam=i2c_arm=on', 'dtparam=i2c_arm=on'), + ('/boot/config.txt', '[all]', '[all]\ngpu_mem=128'), + ('/boot/config.txt', 'camera_auto_detect=1', '#camera_auto_detect=1\nstart_x=1'), + ('/boot/config.txt', 'camera_auto_detect=1', '#camera_auto_detect=1') +] + +for file, initial, new_num in config_modifications: + try: + replace_num(file, initial, new_num) + except Exception as e: + print(f"Error modifying {file}: {e}") + +# Install specific packages +package_commands = [ + "sudo pip3 install --break-system-packages opencv-contrib-python==4.5.3.56", + "sudo pip3 uninstall --break-system-packages -y numpy", + "sudo pip3 install --break-system-packages numpy==1.21", + "sudo apt-get -y install libhdf5-dev libhdf5-serial-dev libatlas-base-dev libjasper-dev", + "sudo pip3 install --break-system-packages imutils zmq pybase64 psutil", + "sudo apt-get install -y util-linux procps hostapd iproute2 iw haveged dnsmasq", + "sudo pip3 install --break-system-packages pi-ina219" +] + +for cmd in package_commands: + run_command(cmd) + +# Clone and install create_ap +if run_command(f"cd {thisPath} && cd .. && sudo git clone https://github.com/oblique/create_ap"): + try: + os.system(f"cd {thisPath} && cd .. && cd create_ap && sudo make install") + except Exception as e: + print(f"Error installing create_ap: {e}") + +# Update rc.local +# replace_num('/etc/rc.local', 'exit 0', f'cd {thisPath} && sudo python3 webServer.py &\nexit 0') print('Completed!') -os.system("sudo reboot") \ No newline at end of file +# Reboot system +# os.system("sudo reboot") diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e69de29