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()
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
It seems like the freezing issue might be related to the fact that you're using
cv2.imshowandcv2.waitKeyinside a loop, and the key event handling might be causing delays. Additionally, thetime.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:
Move
cv2.imshowoutside the loop: Display the image outside the loop to reduce the overhead of updating the display in every iteration.Avoid multiple
cv2.waitKeycalls: Combine your key event checks to a singlecv2.waitKeycall to prevent redundant checks.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.
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.