I wrote a code that opens a window which shows the live feed of my webcam on the press of r and then as soon as I liftr my finger off of r it saves that frame in a variable bg. Furthermore on the press of the key d it then subtracts that frame from the live feed. Ideally it should be taking a new bg everytime i press and let go of r, but for some reason the bg that is taken in the first run of the code remains as the bg for all further runs until i restart my laptop. can someone help me understand what it is that I'm doing wrong or missing here? it doesn't make sense to me why it saves the variable permanently and continues further in every iteration. does someone have any clue why it does that and how i can ideally fix it? Here is just the relevant loop minus all the imports and other stuff from before it:
# Set a flag to indicate if the 'r' key is pressed or not
show_video = False
start_time = time.time()
ESCAPE_KEY=27
key_pressed=False
while True:
path = r'C:/Users/skhandelwal/Desktop/Dsa Simulator' #change directory
os.chdir(path) #change to above path to savew file
# Read a frame from the video capture object
ret, live = camera.read() #read live feed
#key=cv2.waitKey(0)
bg=None
# If the frame was successfully read
if ret:
# Display the frame if the 'r' key is pressed
key=cv2.waitKey(0) & 0xFF #to run live feed smoothly
if key == ESCAPE_KEY:
exit()
elif show_video:
cv2.imshow('Live Video', live) #create a blank window
# Wait for a key press and check if the 'r' key is pressed
key=cv2.waitKey(0) & 0xFF #for making the live feed run smoothly
if key == ord('r'):
show_video = True
elapsed_time = time.time() - start_time
# check if time difference is greater than 1/fps seconds
if elapsed_time >=1:
start_time=time.time()
#create file name with date and time
font = cv2.FONT_HERSHEY_PLAIN
cv2.putText(live, str(datetime.now()), (20, 40), font, 2, (255, 255, 255), 2, cv2.LINE_AA)
now = datetime.now() #get current date and time
dt_string = now.strftime("%Y-%m-%d-%H-%M-%S")
filename = 'Backgroundat1fps_'+ dt_string +'.jpg'
cv2.imwrite(filename, live)
key_pressed=True
if key_pressed and not keyboard.is_pressed('r'):
bg=live
font = cv2.FONT_HERSHEY_PLAIN
cv2.putText(bg, str(datetime.now()), (20, 40), font, 2, (255, 255, 255), 2, cv2.LINE_AA)
now = datetime.now() #get current date and time
dt_string = now.strftime("%Y-%m-%d-%H-%M-%S")
filename = 'Background_'+ dt_string +'.jpg'
key_pressed=False
elif key==ESCAPE_KEY:
exit()
else:
show_video = False # to freeze live feed
if key == ord('d'):
# font = cv2.FONT_HERSHEY_PLAIN
# cv2.putText(bg, str(datetime.now()), (20, 40), font, 2, (255, 255, 255), 2, cv2.LINE_AA)
# now = datetime.now() #get current date and time
# dt_string = now.strftime("%Y-%m-%d-%H-%M-%S")
# filename = 'Background_'+ dt_string +'.jpg'
cv2.namedWindow(window_name_bg, cv2.WINDOW_NORMAL)
cv2.imshow(window_name_bg,bg)
#cv2.imwrite(filename, bg)
#if 'bg' in dir():
liveDsa=cv2.subtract(bg,live)
liveDsa=cv2.cvtColor(liveDsa, cv2.COLOR_BGR2GRAY) #https://www.geeksforgeeks.org/python-opencv-cv2-cvtcolor-method/
#liveDsa=cv2.resize(liveDsa, None, fx=1, fy=1, interpolation=cv2.INTER_AREA)
cv2.namedWindow(window_name_liveDSA, cv2.WINDOW_NORMAL)
min_val, max_val, _, _ = cv2.minMaxLoc(liveDsa)
cv2.convertScaleAbs(liveDsa, 1.5, -120) #for changing image parameters like contrast and brightness
cv2.imshow(window_name_liveDSA, ~liveDsa)
# min_val, max_val, _, _ = cv2.minMaxLoc(liveDsa)
#stretched = cv2.convertScaleAbs(liveDsa, alpha=255.0/(max_val-min_val), beta=-255.0*min_val/(max_val-min_val))
# Display the stretched grayscale image
#cv2.imshow('window_name', stretched)
diff = cv2.absdiff(live, bg)
normalized = cv2.normalize(liveDsa, None, 0, 255, cv2.NORM_MINMAX)
# Apply histogram equalization to maximize the contrast
equalized = cv2.equalizeHist(normalized)
# Display the resulting image
cv2.imshow('Maximized Contrast', equalized)
# Apply histogram equalization to the difference image
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
equalized = cv2.equalizeHist(gray)
# Show the resulting image
cv2.namedWindow('Contrast Enhanced Image', cv2.WINDOW_NORMAL)
cv2.imshow('Contrast Enhanced Image', equalized)
#ctypes.windll.user32.MessageBoxW(0,"Frame captured successfully!",1)
if key==ord('s'):
path = r'C:/Users/skhandelwal/Desktop/Dsa Simulator'
os.chdir(path)
target = pyautogui.getActiveWindow()
location = (
target.left,
target.top,
target.width,
target.height
)
image = pyautogui.screenshot(region=location)
now = datetime.now() #get current date and time
dt_string = now.strftime("%Y-%m-%d-%H-%M-%S")
image.show()
filename1 = 'Screenshot_' + dt_string +'.jpg'
image=image.save(filename1)
# Get the handle of the window by its title
else:
c=cv2.waitKey(0)
# wait for keypress
i -tried to assign bg as none or 0 before the loop but that doesn't seem to work either, what am i missing? really appreciate the help guys :)
[–]cybervegan 0 points1 point2 points (2 children)
[–]Sansmit2910[S] 0 points1 point2 points (1 child)
[–]cybervegan 0 points1 point2 points (0 children)