While saving the photograph app freezes and takes long time to save in MacOS

53 views Asked by At

Here is my code:

    import os
    import cv2
    import uuid
    import time


    camera = cv2.VideoCapture(0)

    POZ_Dosya = os.path.join("veriler", "pozitif")
    NEG_Dosya = os.path.join("veriler", "negatif")
    Temel_Dosya = os.path.join("veriler", "temel")
    os.makedirs(POZ_Dosya, exist_ok=True)
    os.makedirs(NEG_Dosya, exist_ok=True)
    os.makedirs(Temel_Dosya, exist_ok=True)

    target_size = 250

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

        scale_percent = 33 
        width = int(frame.shape[1] * scale_percent / 100)
        height = int(frame.shape[0] * scale_percent / 100)
        dim = (width, height)
        frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)

        height, width, _ = frame.shape

        start_row = max(0, int((height - target_size) / 2))
        end_row = min(height, start_row + target_size)
        start_col = max(0, int((width - target_size) / 2))
        end_col = min(width, start_col + target_size)

        frame = frame[start_row:end_row, start_col:end_col, :]
        frame = cv2.resize(frame, (target_size, target_size))
        cv2.imshow("a", frame)
        
        if cv2.waitKey(1) & 0xFF == ord("a"):
            time.sleep(10)
            imagename = os.path.join(Temel_Dosya, '{}.jpg'.format(uuid.uuid1()))
            cv2.imwrite(imagename, frame)
        
        if cv2.waitKey(1) & 0xFF == ord("p"):
            pass
        
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
        
    camera.release()
    cv2.destroyAllWindows()


My computer

How can I solve this.

Thanks in advance.

I was trying to take lots of photos for a dataset but it freezes when saving and takes long time to save. I have added

 "editor.formatOnSave": false,
 "editor.codeActionsOnSave": {
        "source.organizeImports": "never"

to my setting.js on VS code still it keeps contuing

1

There are 1 answers

3
Muhammad Nabeegh On BEST ANSWER

It seems like the freezing issue might be related to the fact that you're using cv2.imshow and cv2.waitKey inside a loop, and the key event handling might be causing delays. Additionally, the time.sleep(10) after saving an image could be contributing to the freezing.

Here are a few suggestions to optimize and potentially solve the freezing issue:

  1. Move cv2.imshow outside the loop: Display the image outside the loop to reduce the overhead of updating the display in every iteration.

    while camera.isOpened():
        # ... (your existing code)
        cv2.imshow("a", frame)
    cv2.destroyAllWindows()
    
  2. Avoid multiple cv2.waitKey calls: Combine your key event checks to a single cv2.waitKey call to prevent redundant checks.

    while camera.isOpened():
        # ... (your existing code)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("a"):
            time.sleep(10)
            imagename = os.path.join(Temel_Dosya, '{}.jpg'.format(uuid.uuid1()))
            cv2.imwrite(imagename, frame)
        elif key == ord("p"):
            pass
        elif key == ord("q"):
            break
        cv2.imshow("a", frame)
    cv2.destroyAllWindows()
    
  3. Check for key events only when necessary: Instead of checking for key events in every iteration, you can move the key event handling to specific conditions where you want to capture an image.

    while camera.isOpened():
        # ... (your existing code)
        cv2.imshow("a", frame)
    
        key = cv2.waitKey(1) & 0xFF
        if key == ord("a"):
            time.sleep(10)
            imagename = os.path.join(Temel_Dosya, '{}.jpg'.format(uuid.uuid1()))
            cv2.imwrite(imagename, frame)
        elif key == ord("q"):
            break
    
    cv2.destroyAllWindows()
    

Make sure to test these changes and see if the freezing issue persists. If the problem continues, you may need to explore more specific profiling and debugging techniques to identify the bottleneck in your code.