-1

I want to ask how to change the color of Bounding Box and Font of a detected object when it is past a line. I am currently working on a project on human walking speed estimation by using Haar-cascade. The program works as such: the detected object passes two imaginary lines and when it passes the second line the program will show the speed. If the speed of the detected humans is below 3 km/h, the Bounding Box and the font will be shown in RED, but if it is more than 3 km/h it will be shown in GREEN. And I want the text of the speed to be shown for 5 seconds.

Hope you can help me solve this. Here's the program that I've worked on.

import time

cascade_src = 'haarcascade_fullbody.xml'
video_src = 'video-1.mp4'

#line a
ax1=15
ay=225
ax2=600

#line b
bx1=15
by=275
bx2=600

#car num
i = 1
start_time = time.time()

#video ....
cap = cv2.VideoCapture(video_src)
human_cascade = cv2.CascadeClassifier(cascade_src)   

videoWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
videoHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('wisuda-14.mp4', fourcc, 25.0, (videoWidth,videoHeight))

def Speed_Cal(time):
    try:
        Speed = (9.144*3600)/(time*1000)
        return Speed
    except ZeroDivisionError:
        print (5)

while True:
    ret, img = cap.read()
    if (type(img) == type(None)):
        break
    
    #bluring to have exacter detection
    blurred = cv2.blur(img, ksize=(3,3))
    gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
    human = human_cascade.detectMultiScale(gray, scaleFactor=1.04865, minNeighbors=6)
    
    #line a #i know road has got 
    cv2.line(img,(ax1,ay),(ax2,ay),(255,0,0),2)
    #line b
    cv2.line(img,(bx1,by),(bx2,by),(255,0,0),2)
    
    for (x,y,w,h) in human:
        cv2.rectangle(img, (x,y), (x + w, y + h), (0, 0, 255), 2)
        roi_blurred = blurred[x: x + h, y:y + w]
        roi_gray = gray[x: x + h, y:y + w]
        roi_img = img[x: x + h, y:y + w]
        cv2.circle(img,(int((x+x+w)/2),int((y+y+h)/2)), 2,(0,255,0), -1)
        #cv2.putText(img, "ID : " + str(i), (x, y-15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1);
        
        while int(ay) == int((y+y+h)/2):
            start_time = time.time()
            break
            
        while int(ay) <= int((y+y+h)/2):
            if int(by) <= int((y+y+h)/2)&int(by+10) >= int((y+y+h)/2):
                cv2.line(img, (bx1,by), (bx2,by), (0,255,0), 2)
                Speed = Speed_Cal(time.time() - start_time)
                print("ID Number "+str(i)+" Speed: " + str(int(Speed)))
                i = i + 1
                cv2.putText(img, "Speed: "+str(int(Speed))+"km/jam", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2);
                break
            else :        
                break
                
                
    out.write(img)
    cv2.imshow('video', img)
    cv2.imshow('Gray', gray)
    cv2.imshow('Blurr', blurred)
    
    if cv2.waitKey(33) == 27:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

I do really hope you can help me guys, please.

1 Answers1

1

cv2.rectangle(img, (x,y), (x + w, y + h), (0, 0, 255), 2) the tuple with 3 elements: (0, 0, 255) is correspondent to the RGB (or BGR I forgot) value of the bounding rectangle, changing the values will change the color. For more information on bounding rectangles, check out the OpenCV drawing functions doc: https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html

As for the text color, cv2.putText(img, "Speed: "+str(int(Speed))+"km/jam", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2), changing the tuple (255,0,0) will change the text color.

import time
start = time.time()
sec = 5
while True:
    if condition:
        start = time.time()
    if time.time() - start < sec:
        #do whatever
Bob
  • 236
  • 1
  • 4
  • Yes, I am aware on change the BGR thank you. But I want the color changes permanent after the detected object pass the second line. How to do it? – Agil Calfarera Aug 26 '20 at 04:54
  • You could set a tuple that holds the BGR value, then when the object is detected, you can replace the values with a new set of values. – Bob Aug 27 '20 at 02:35
  • Thank you it's working, but after the object move further the line, the colors change again to blue. How to make it change permanently or for a while? – Agil Calfarera Aug 28 '20 at 07:47
  • You could use a timer. When you the object is detected, do `start = time.time()`, and every time you go through the while loop, check if the time exceeds the desired amount by doing `time.time() - start`. – Bob Aug 29 '20 at 21:06
  • I'm never use timer before, can you help me give more references that could help me? It will really helpful thank you. – Agil Calfarera Aug 30 '20 at 06:04
  • Okay thank you I'll try it. Anyway can you see my other questions? I need your help badly. Thanks in advance. – Agil Calfarera Sep 01 '20 at 00:30