hushdavid
Posts: 4
Joined: Sun Aug 06, 2023 5:20 pm

Correct loop needed

Thu Sep 28, 2023 2:16 pm

Hey all,
I've been messing with this script for a few weeks now.
What I'm trying to do is: If motion is detected > run object detection script > if a cup is detected play audio file > if a cup IS NOT detected after a period of time > cancel script and wait until motion is detected

I believe it's a loop fix but I'm new to Python :D Any help is appreciated!

Here is my code:

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import cv2
import pygame
from pygame import mixer

# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_PIR = 21

print ("PIR Module Test (CTRL-C to exit)")

# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo

Current_State  = 0
Previous_State = 0

try:

  print ("Waiting for PIR to settle ...")

  # Loop until PIR output is 0
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0    

  print ("  Ready")     
    
  # Loop until users quits with CTRL-C
  while True :
   
    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)
   
    if Current_State==1 and Previous_State==0:
      # PIR is triggered
      print ("  Motion detected!")
      
      
      classNames = []
      classFile = "/home/pi/Desktop/Object_Detection_Files/coco.names"
      #classFile = "/home/pi/coco.names"
      with open(classFile,"rt") as f:
            classNames = f.read().rstrip("\n").split("\n")

      configPath = "/home/pi/Desktop/Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
      weightsPath = "/home/pi/Desktop/Object_Detection_Files/frozen_inference_graph.pb"

      net = cv2.dnn_DetectionModel(weightsPath,configPath)
      net.setInputSize(320,320)
      net.setInputScale(1.0/ 127.5)
      net.setInputMean((127.5, 127.5, 127.5))
      net.setInputSwapRB(True)


      def getObjects(img, thres, nms, draw=True, objects=[]):
            classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
            #print(classIds,bbox)
            if len(objects) == 0: objects = classNames
            objectInfo =[]
            if len(classIds) != 0:
                for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
                    className = classNames[classId - 1]
                    if className in objects:
                        objectInfo.append([box,className])
                        #if (draw):
                           # cv2.rectangle(img,box,color=(0,255,0),thickness=2)
                           # cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30),
                           # cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
                           # cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30),
                           # cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)

            return img,objectInfo


      if __name__ == "__main__":

            mixer.init()
            mixer.music.load("clap.wav")
            mixer.music.set_volume(1.0)
            
            cap = cv2.VideoCapture(-2)
            cap.set(3,640)
            cap.set(4,480)
            cap.set(10,70)


            while True:
                success, img = cap.read()
                result, objectInfo = getObjects(img,0.45,0.2, objects=['cup'])
                
                if objectInfo and not pygame.mixer.music.get_busy():
                    mixer.music.play()
                    
                print(objectInfo)
                
                
                # Display video output
                # cv2.imshow("Output",img)
                # cv2.waitKey(1)
      
      # Record previous state
      Previous_State=1
    elif Current_State==0 and Previous_State==1:
      # PIR has returned to ready state
      print ("  Ready")
      Previous_State=0
      
    # Wait for 10 milliseconds
    time.sleep(0.10)      
   
except KeyboardInterrupt:
  print ("  Quit" )
  # Reset GPIO settings
  GPIO.cleanup()

dbrion1
Posts: 196
Joined: Tue May 30, 2023 4:42 pm
Location: North of France

Re: Correct loop needed

Mon Oct 02, 2023 2:26 pm

Well, though I am not proficient in python, I seee some issues in your script:

* each time "you" (PIR) detects there is something, you load a neural coconet basd DNN and a globaal varible named "net" is created from the disk..
As it takes time to load a DNN (it is ca 10 M- if file is opened, it may take 0.1 -1 second) you should load it once, at the beginning.

* you use active loops to detect that the GPIO is rising; code might be simpler by using functions such as wait_for_edge, https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/ -if this is the same library- (code would be easier to read)

* when posting code, you should not show functions (from opencv python) with a lot of lines commented (the commented lines display the image, show the diagnostics with a nice font: that makes a lot of distracting lines).
OTOH, printing (at least at the debugging phase) wheteher the detector detected and what it detected would be useful...

BTW:
*did your video capure work?
* what is the size of the video? input stage of the detector?

hippy
Posts: 15200
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Correct loop needed

Mon Oct 02, 2023 3:43 pm

hushdavid wrote:
Thu Sep 28, 2023 2:16 pm
What I'm trying to do is: If motion is detected > run object detection script > if a cup is detected play audio file > if a cup IS NOT detected after a period of time > cancel script and wait until motion is detected
I would do something like this ...

Code: Select all

EnableMotionDetector()
while True:
  if MotionDetected():
    EnableObjectIdentification()
    startTime = time.time()
    while time.time() - startTime < detectPeriodSeconds:
      if CupDetected():
        IndicateCupHasBeenDetected()
        while CupDetected():
          pass
        startTime = time.time()
    CancelObjectDetection()
That 'time.time() - startTime' may need to be more robust to handle timer wraparound.

hushdavid
Posts: 4
Joined: Sun Aug 06, 2023 5:20 pm

Re: Correct loop needed

Thu Oct 05, 2023 12:58 pm

Thank you for your response!

* each time "you" (PIR) detects there is something, you load a neural coconet basd DNN and a globaal varible named "net" is created from the disk..
As it takes time to load a DNN (it is ca 10 M- if file is opened, it may take 0.1 -1 second) you should load it once, at the beginning.
A lot of this code was pulled from https://core-electronics.com.au/guides/ ... pberry-pi/ so there was some hack and slash happening. Are you recommending that I place the net global up top after the imports?

* you use active loops to detect that the GPIO is rising; code might be simpler by using functions such as wait_for_edge, https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/ -if this is the same library- (code would be easier to read) Thank you for the share!

* when posting code, you should not show functions (from opencv python) with a lot of lines commented (the commented lines display the image, show the diagnostics with a nice font: that makes a lot of distracting lines).
OTOH, printing (at least at the debugging phase) wheteher the detector detected and what it detected would be useful...
I will do this moving forward. This is a new world for me :)

BTW:
*did your video capure work? Yes. I did comment out the video capture for resource management
* what is the size of the video? input stage of the detector? I believe this is the code for reference: net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)

hushdavid
Posts: 4
Joined: Sun Aug 06, 2023 5:20 pm

Re: Correct loop needed

Thu Oct 05, 2023 1:01 pm

hippy wrote:
Mon Oct 02, 2023 3:43 pm
I would do something like this ...

Code: Select all

EnableMotionDetector()
while True:
  if MotionDetected():
    EnableObjectIdentification()
    startTime = time.time()
    while time.time() - startTime < detectPeriodSeconds:
      if CupDetected():
        IndicateCupHasBeenDetected()
        while CupDetected():
          pass
        startTime = time.time()
    CancelObjectDetection()
That 'time.time() - startTime' may need to be more robust to handle timer wraparound.
Thanks for the framework. I will do my best to apply it.

Return to “Python”