Quantcast
Viewing all articles
Browse latest Browse all 14389

Playing sound file only once on object detection in opencv

The below program is an implementation of Air drums using openCV. Blue colored objects are considered as drumsticks, and when these objects are present in the region of interest, then the sound is played. I want the sound to play only once.
As of now, the sound keeps looping and playing back, if I just keep my drumstick in the region of interest(which is what the code is designed to do), but i want to improve it.
I was thinking if there was some way to make the sound play only if the drumstick goes away from the region of interest and comes back.
Any resources or help would be appreciated. Here is the code:

import numpy as npimport timeimport cv2import pygamedef state_machine(sumation,sound):    # Check if blue color object present in the ROI         yes = (sumation) > Hatt_thickness[0]*Hatt_thickness[1]*0.8    # If present play the respective instrument.    if yes and sound==1:        drum_clap.play()    elif yes and sound==2:        drum_snare.play()def ROI_analysis(frame,sound):    # converting the image into HSV    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)    # generating mask for     mask = cv2.inRange(hsv, blueLower, blueUpper)    # Calculating the nuber of white pixels depecting the blue color pixels in the ROI    sumation = np.sum(mask)    # Function that decides to play the instrument or not.    state_machine(sumation,sound)    return maskVerbsoe = False# importing the audio filespygame.init()drum_clap = pygame.mixer.Sound('batterrm.wav')drum_snare = pygame.mixer.Sound('button-2.ogg')# HSV range for detecting blue color blueLower = (80,150,10)blueUpper = (120,255,255)# Frame accusition from webcam/ usbcamera camera = cv2.VideoCapture(0)ret,frame = camera.read()H,W = frame.shape[:2]kernel = np.ones((7,7),np.uint8)# reading the image of hatt and snare for augmentation.Hatt = cv2.resize(cv2.imread('./Images/Hatt.png'),(200,100),interpolation=cv2.INTER_CUBIC)Snare = cv2.resize(cv2.imread('./Images/Snare.png'),(200,100),interpolation=cv2.INTER_CUBIC)# Setting the ROI area for blue color detectionHatt_center = [np.shape(frame)[1]*2//8,np.shape(frame)[0]*6//8]Snare_center = [np.shape(frame)[1]*6//8,np.shape(frame)[0]*6//8]Hatt_thickness = [200,100]Hatt_top = [Hatt_center[0]-Hatt_thickness[0]//2,Hatt_center[1]-Hatt_thickness[1]//2]Hatt_btm = [Hatt_center[0]+Hatt_thickness[0]//2,Hatt_center[1]+Hatt_thickness[1]//2]Snare_thickness = [200,100]Snare_top = [Snare_center[0]-Snare_thickness[0]//2,Snare_center[1]-Snare_thickness[1]//2]Snare_btm = [Snare_center[0]+Snare_thickness[0]//2,Snare_center[1]+Snare_thickness[1]//2]time.sleep(1)while True:    # grab the current frame    ret, frame = camera.read()    frame = cv2.flip(frame,1)    if not(ret):        break    # Selecting ROI corresponding to snare    snare_ROI = np.copy(frame[Snare_top[1]:Snare_btm[1],Snare_top[0]:Snare_btm[0]])    mask = ROI_analysis(snare_ROI,1)    # Selecting ROI corresponding to Hatt    hatt_ROI = np.copy(frame[Hatt_top[1]:Hatt_btm[1],Hatt_top[0]:Hatt_btm[0]])    mask = ROI_analysis(hatt_ROI,2)    # A writing text on an image.    cv2.putText(frame,'Project: Air Drums',(10,30),2,1,(20,20,20),2)    # Display the ROI to view the blue colour being detected    if Verbsoe:        # Displaying the ROI in the Image        frame[Snare_top[1]:Snare_btm[1],Snare_top[0]:Snare_btm[0]] = cv2.bitwise_and(frame[Snare_top[1]:Snare_btm[1],Snare_top[0]:Snare_btm[0]],frame[Snare_top[1]:Snare_btm[1],Snare_top[0]:Snare_btm[0]], mask=mask[Snare_top[1]:Snare_btm[1],Snare_top[0]:Snare_btm[0]])        frame[Hatt_top[1]:Hatt_btm[1],Hatt_top[0]:Hatt_btm[0]] = cv2.bitwise_and(frame[Hatt_top[1]:Hatt_btm[1],Hatt_top[0]:Hatt_btm[0]],frame[Hatt_top[1]:Hatt_btm[1],Hatt_top[0]:Hatt_btm[0]],mask=mask[Hatt_top[1]:Hatt_btm[1],Hatt_top[0]:Hatt_btm[0]])    # Augmenting the instruments in the output frame.    else:        # Augmenting the image of the instruments on the frame.        frame[Snare_top[1]:Snare_btm[1],Snare_top[0]:Snare_btm[0]] = cv2.addWeighted(Snare, 1, frame[Snare_top[1]:Snare_btm[1],Snare_top[0]:Snare_btm[0]], 1, 0)        frame[Hatt_top[1]:Hatt_btm[1],Hatt_top[0]:Hatt_btm[0]] = cv2.addWeighted(Hatt, 1, frame[Hatt_top[1]:Hatt_btm[1],Hatt_top[0]:Hatt_btm[0]], 1, 0)    cv2.imshow('Output',frame)    key = cv2.waitKey(1) & 0xFF    # if the 'q' key is pressed, stop the loop    if key == ord("q"):        break# cleanup the camera and close any open windowscamera.release()cv2.destroyAllWindows()

Viewing all articles
Browse latest Browse all 14389

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>