Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in loop #3

Closed
zizouk59 opened this issue Apr 14, 2022 · 5 comments
Closed

Problem in loop #3

zizouk59 opened this issue Apr 14, 2022 · 5 comments

Comments

@zizouk59
Copy link

Hello I have a Problem !
I putted the rectangle ROI in a loop but, it works only once, for the second time : i got the message for selecting ROI, but the window didn't show.
Capture d’écran 2022-04-14 à 16 10 14

@saharshleo
Copy link
Owner

can you please post the code too (the loop part)

@zizouk59
Copy link
Author

@saharshleo
print('img shape', img_arr.shape)
roi_helper=EasyROI(verbose=True)
rect_roi=roi_helper.draw_rectangle(img_arr, 1)
print(rect_roi)
roii=rect_roi['roi']
print(roii)
roii=roii[0]

# process your img_arr here    

#cv2.imwrite('img.png', img_arr)
#r = cv2.selectROI("select the area",img_arr)
#cv2.imshow("frame", r)
#key=cv2.waitKey(0)
#if key & 0xFF == ord('q'):
#	cv2.destroyAllWindows()
#cv2.imshow("img",im)
#cv2.waitKey(10)    
# access other keys of json
# print(request.json['other_key'])
x1 = roii['br_x']
y1 = roii['br_y']
x2 = roii['tl_x']
y2 = roii['tl_y']
result_dict = {'x1':x1 ,'y1': y1,'x2': x2,'y2': y2}
print(result_dict)

@zizouk59
Copy link
Author

this code is inside the loop (while True)

@saharshleo
Copy link
Owner

which part of code is inside while True?,
what is your requirement exactly?, if you want to select multiple rectangles on single frame then you can pass that as a parameter in roi_helper.draw_rectangle(frame, 2). Here 2 is the quantity of rectangles.

however if you want to select rectangles on different frames, below is the working code:

from EasyROI import EasyROI

import cv2
from pprint import pprint


if __name__=='__main__':
    roi_helper = EasyROI(verbose=True)
    video_path = 'input/overpass.mp4'

    # Initialize cam
    cap = cv2.VideoCapture(video_path)
    assert cap.isOpened(), 'Cannot capture source'

    while(cap.isOpened()):
        ret, frame = cap.read()

        if ret == True:
            # DRAW RECTANGULAR ROI
            rect_roi = roi_helper.draw_rectangle(frame, 1)
            print("Rectangle Example:")
            pprint(rect_roi)

            frame_temp = roi_helper.visualize_roi(frame, rect_roi)
            cv2.imshow("frame", frame_temp)
            key = cv2.waitKey(0)
            if key & 0xFF == ord('q'):
                cv2.destroyAllWindows()

            if cv2.waitKey(5000) & 0xFF == 27:  # wait for ESC key for 5seconds
                break
        
        else:
            break

    cap.release()
    cv2.destroyAllWindows()

Output:

[DEBUG] Welcome to easyROI
[DEBUG] Entered draw_rectangle
Select a ROI and then press SPACE or ENTER button!
Cancel the selection process by pressing c button!

Rectangle Example:
{'roi': {0: {'br_x': 395,
             'br_y': 489,
             'h': 58,
             'tl_x': 245,
             'tl_y': 431,
             'w': 150}},
 'type': 'rectangle'}
[DEBUG] Entered draw_rectangle
Select a ROI and then press SPACE or ENTER button!
Cancel the selection process by pressing c button!

Rectangle Example:
{'roi': {0: {'br_x': 485,
             'br_y': 556,
             'h': 35,
             'tl_x': 260,
             'tl_y': 521,
             'w': 225}},
 'type': 'rectangle'}
[DEBUG] Entered draw_rectangle
Select a ROI and then press SPACE or ENTER button!
Cancel the selection process by pressing c button!

Rectangle Example:
{'roi': {0: {'br_x': 407,
             'br_y': 588,
             'h': 89,
             'tl_x': 195,
             'tl_y': 499,
             'w': 212}},
 'type': 'rectangle'}
[DEBUG] Entered draw_rectangle
Select a ROI and then press SPACE or ENTER button!
Cancel the selection process by pressing c button!

[DEBUG] Not all ROI's drawn
Rectangle Example:
{}
Thank You!!

@zizouk59
Copy link
Author

zizouk59 commented May 9, 2022

Thank you !
I fixed my problem.
I was running a Flask Server with threads.
There is an issue you can only run the selectroi on the first thread.
So, I desactivated the threads, and it works!!

@zizouk59 zizouk59 closed this as completed May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants