Text written on the frame also turns gray when video frames turned into grayscale

126 views Asked by At

I am building a motion detector application.So for the motion detection algorithm to work, I converted the frames to grayscale so now the application is able to detect the motions.But when I try to put a text on the frame trying to post a message like "MOVING", even the text has turned gray and is hardly visible.How do I draw colored text on a video frame?

Below is my motion detection application code

import cv2
import numpy as np
from skimage.measure import compare_ssim
from twilio.rest import Client

#we can compare two images using Structural Similarity
#so a small change in pixel value won't prompt this method to term both images as dissimilar
#the closer the value is to 1,the more similar two images are
def ssim(A, B):
    return compare_ssim(A, B, data_range=A.max() - A.min())

#capture a video either from a file or a live video stream
cap = cv2.VideoCapture(0)
first_frame = True
prev_frame = None
current_frame = None
#we keep a count of the frames
frame_counter = 0
while True:
    if frame_counter == 0:
        #prev_frame will always trail behind the current_frame
        prev_frame = current_frame
    #get a frame from the video
    ret, current_frame = cap.read()
    #if we reach the end of the video in case of a video file,stop reading
    if current_frame is None:
        break

    #convert the image to grayscale
    current_frame = cv2.cvtColor(current_frame,cv2.COLOR_BGR2GRAY)
    if first_frame:
        #for the first time prev_frame and current_frame will be the same
        prev_frame = current_frame
        first_frame = False

    if frame_counter == 9:
        #compare two images based on SSIM
        ssim_val = ssim(current_frame, prev_frame)
        print(ssim_val)
        #if there is a major drop in the SSIM value ie it has detected an object
        if ssim_val < 0.8:
            # Here I want to put a colored text to the screen
            cv2.putText(current_frame, "MOVING", (100, 300),
                        cv2.FONT_HERSHEY_TRIPLEX, 4, (255, 0, 0))
        frame_counter = -1

    #show the video as a series of frames
    cv2.imshow("Motion Detection",current_frame) #(name of the window,image file)
    frame_counter += 1

    key = cv2.waitKey(1) & 0xFF  #cv2.waitKey(1) returns a value of -1 which is masked using & 0xFF to get char value
    if key == ord('q'): #gives ASCII value of 'q'
        break

#release the resources allocated to the video file or video stream
cap.release()
#destroy all the windows
cv2.destroyAllWindows()

I searched online and I got this piece of code which basically suggests to convert grayscale back to BGR

backtorgb = cv2.cvtColor(current_frame, cv2.COLOR_GRAY2RGB)

But this didn't work.I even took a copy of the current frame before it being converted to grayscale frame and then tried to write on the copied color frame but still the text comes gray and not colored.What should I do?

0

There are 0 answers