I have the code below which I try to do the following: Starts capturing the camera image but wanted to also display a dialog box containing a text field and an "ok" button, where the user must enter a value and pressing the "ok" button then scritp saves the image (jpg) naming it with the value that the user passed.
import cv2
import time
import pyautogui
camera_port = 0
camera = cv2.VideoCapture(camera_port)
camera.set(3, 1280)# set the resolution
camera.set(4, 1024)
emLoop = True
while (emLoop):
retval, img = camera.read()
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('Foto', img)
k = cv2.waitKey(100)
if k == 27:
emLoop = False
elif k == ord('s'):
nome = input(pyautogui.prompt(text='Scanei o código.', title='Aviso:', default=''))
cv2.imwrite(nome, img)
camera.release()
cv2.destroyAllWindows()
Capture would always be active and as long as the user was entering the values and clicking "ok" would save the images, when the user clicked the "cancel" dialog box, would end the capture and terminate the script. I'm trying to do this but unsuccessfully so far ... :(
The function pyautogui.prompt
opens a dialog box with a text field where the user can type something that will be returned. This is similar to how the function input
works. Both have a string as an argument to be displayed as a prompt message to the user. The difference is that the former uses the terminal as the input interface instead of a dialog box.
It looks weird mixing them the way you are trying: nome = input(pyautogui.prompt(...))
. It means like: Open a dialog box so the user can type a prompt message that will be displayed for himself in the console. After that, he types again something that will be returned to the nome
variable.
Try either this:
nome = pyautogui.prompt(text='Scanei o código.', title='Aviso:', default='')
Or this:
nome = input('Aviso: Scanei o código.')