To run another function when a keystroke event occurs while executing a repeat statement in Python

Asked 2 years ago, Updated 2 years ago, 25 views

Python code that receives the image folder and blurs the dragged area by floating each image in the folder.

[Image 1, image 2, image 3, image 4, image 5] If you have a list of images like this, During image 4 work, I want to implement the function of moving to image 3, which is the previous image, by pressing the d key, and working again, that is, .

We tried to wait for the keydown with a keyboard.read_key in the for or while statement, but this method could not move from the line waiting for the keydown to the next line.

I think there should be a way to get out without executing any more when the key down event occurs during the execution of the while statement, but is this possible in Python?

Or are there other ways to study?

Please point it out! Thank you

import cv2
import os
ksize = 30              
win_title = 'deidentify'    

process_path = input ('De-identification Processing Target Image Folder Path')
img_path_list = os.listdir(process_path)
img_path_list.sort()
img_path_list = [os.path.join(process_path,img) for img in img_path_list] 
result_path = input ('path to the folder where de-identification results are stored')

for img in img_path_list:
    file_name = os.path.basename(img)
    img = cv2.imread(img)    

    while True:
        x,y,w,h = cv2.selectROI(win_title, img, False) 
        if w > 0 and h > 0:          
            roi = img[y:y+h, x:x+w]   
            roi = cv2.blur(roi, (ksize, ksize)) 
            img[y:y+h, x:x+w] = roi   
            cv2.imshow(win_title, img)
        else:
            cv2.imwrite(os.path.join(result_path, file_name), img)
            break


cv2.destroyAllWindows()

python

2022-09-19 23:32

2 Answers

You're using cv2, I think you can implement the function you want through the cv2.waitKey() method.

import cv2
import os
MILLI_SEC = 100  # 100ms
ksize = 30              
win_title = 'deidentify'    

process_path = input ('De-identification Processing Target Image Folder Path')
img_path_list = os.listdir(process_path)
img_path_list.sort()
img_path_list = [os.path.join(process_path,img) for img in img_path_list] 
result_path = input ('path to the folder where de-identification results are stored')

for img in img_path_list:
    file_name = os.path.basename(img)
    img = cv2.imread(img)    

    while True:
        # The waitKey function waits by ms and returns the entered key.
        # Returns -1 if the key is not pressed.
        # If delay(ms) is negative, wait indefinitely.
        key = cv2.waitKey(MILLI_SEC)
        if key == ord('d'):
            break

        x,y,w,h = cv2.selectROI(win_title, img, False) 
        if w > 0 and h > 0:          
            roi = img[y:y+h, x:x+w]   
            roi = cv2.blur(roi, (ksize, ksize)) 
            img[y:y+h, x:x+w] = roi   
            cv2.imshow(win_title, img)
        else:
            cv2.imwrite(os.path.join(result_path, file_name), img)
            break


cv2.destroyAllWindows()

Note: https://docs.opencv.org/4.x/d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7


2022-09-19 23:32

I think it will be possible only if it is made into multi-threaded or multi-processed.
You can receive keystrokes from the main thread (or main process) and proceed with the work from the sub-thread (or sub-process).


2022-09-19 23:32

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.