Quantcast
Viewing all articles
Browse latest Browse all 4724

Troubleshooting • Keep Restarting

Hello, forum

So I am using Raspberry Zero 2W and whenever I run some code in Python on my board it stopped responding and then it starts to blink a green LED on the board, I can't do anything I have to unplug the board then again I have to start. can you tell me what the problem is going on for your info I am attaching the code too so you get better idea.

Code:

import cv2from picamera.array import PiRGBArrayfrom picamera import PiCameraimport timefrom datetime import datetime, timedeltafrom telegram.ext import Updater, CommandHandler, MessageHandler, Filtersimport threadingimport os# Telegram Bot SetupTOKEN = 'Token'PASSWORD = 'password'  # Define a password for authenticationAUTHORIZED_USERS = []  # List to store authenticated usersdef start(update, context):    update.message.reply_text('Welcome! Please authenticate by sending the password.')def authenticate(update, context):    user_id = update.message.from_user.id    text = update.message.text    if text == PASSWORD:        if user_id not in AUTHORIZED_USERS:            AUTHORIZED_USERS.append(user_id)            update.message.reply_text('Authentication successful. You will now receive notifications.')        else:            update.message.reply_text('You are already authenticated.')    else:        update.message.reply_text('Incorrect password. Please try again.')def setup_telegram_bot():    updater = Updater(TOKEN)    dp = updater.dispatcher    dp.add_handler(CommandHandler("start", start))    dp.add_handler(MessageHandler(Filters.text & ~Filters.command, authenticate))    updater.start_polling()    updater.idle()# Start the Telegram bot in a separate threadbot_thread = threading.Thread(target=setup_telegram_bot)bot_thread.start()# Function to send notifications with videodef send_notification(bot, video_file):    for user_id in AUTHORIZED_USERS:        bot.send_message(chat_id=user_id, text="Face detected! Sending a 5-second clip.")        bot.send_video(chat_id=user_id, video=open(video_file, 'rb'))# Initialize PiCameracamera = PiCamera()camera.resolution = (640, 480)camera.framerate = 24rawCapture = PiRGBArray(camera, size=(640, 480))# Allow the camera to warm uptime.sleep(2)# Load the Haar Cascade for face detectionface_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')# Initialize variablesrecord = Falseout = Noneno_face_detected_counter = 0grace_period = 24  # Frames to wait before stopping recording after face disappearsframe_skip = 0  # Process every n-th frame to improve speed; set to 1 to process every frameframe_count = 0try:    # Capture frames from the camera    for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):        frame_count += 1        if frame_count % frame_skip != 0:  # Skip frames to improve processing speed            rawCapture.truncate(0)            continue        image = frame.array        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)        gray = cv2.equalizeHist(gray)  # Improve detection in darker areas        # Detect faces in the image        faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))        if len(faces) > 0:            no_face_detected_counter = 0            if not record:                record = True                start_time = datetime.now()                filename = f"video_{start_time.strftime('%Y-%m-%d_%H-%M-%S')}.mp4"                fourcc = cv2.VideoWriter_fourcc(*'mp4v')                out = cv2.VideoWriter(filename, fourcc, 24.0, (640, 480))            for (x, y, w, h) in faces:                cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2)        else:            no_face_detected_counter += 1            if no_face_detected_counter > grace_period and record:                record = False                out.release()                out = None                if datetime.now() - start_time >= timedelta(seconds=5):                    send_notification(filename)                else:                    os.remove(filename)        if record:            out.write(image)        cv2.imshow('Frame', image)        rawCapture.truncate(0)        if cv2.waitKey(1) & 0xFF == ord('q'):            breakexcept KeyboardInterrupt:    print("Interrupt received, stopping...")finally:    if out:        out.release()    cv2.destroyAllWindows()    print("Cleaned up resources.")

Statistics: Posted by PrinceKakkad — Sat Jul 20, 2024 11:56 am — Replies 0 — Views 12



Viewing all articles
Browse latest Browse all 4724

Trending Articles