Detect a key pressed in dlib.image_window() - python-3.x

Having the following code:
import dlib
import cv2
from lib.capture import Capture
win = dlib.image_window()
cap = Capture() # Capture image from webcam
cap.start()
while(True):
frame = cap.get() # Get current frame from webcam
if frame is not None:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Converting from RGB to BGR (as dlib.image_window requires)
win.set_image(frame) # Display the resulting frame
How can I detect a key pressed e.g. "ESC" in the dlib window???

You can use this:
from msvcrt import getch
import dlib
import cv2
from lib.capture import Capture
win = dlib.image_window()
cap = Capture() # Capture image from webcam
cap.start()
while(True):
frame = cap.get() # Get current frame from webcam
if frame is not None:
frame = cv2.flip(frame, 1)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) # Converting from RGB to BGR (as dlib.image_window requires)
win.set_image(frame) # Display the resulting frame
key = ord(getch())
if key == 27: #ESC
break

Related

Is there a way to tell if pyautogui is detecting the color/clicking?

import cv2
import numpy as np
import pyautogui
SCREEN_SIZE = (1920, 1080)
#define the codec
fourcc = cv2.VideoWriter_fourcc(*"XVID")
#create the video write object
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (SCREEN_SIZE))
while True:
#make a screenshot
img = pyautogui.screenshot(region=(680, 785, 560, 20))
#convert these pixels to a proper numpy array to work with OpenCV
frame = np.array(img)
#convert colors from BGR to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
black = [0,0,0]
for x in range(img.width):
for y in range(img.height):
if img.getpixel((x, y)) == black:
print(x, y)
pyautogui.click(x, y)
#write the frame
out.write(frame)
#show the frame
cv2.imshow("screenshot", frame)
# if the user clicks q, it exits
if cv2.waitKey(1) == ord("q"):
break
# make sure everything is closed when exited
cv2.destroyAllWindows()
out.release()
I am creating a script to detect black squares, and click them. For some reason when using this, there is no error, but it is not clicking. Is there a way to tell whether it is detecting the color/clicking?
Edit: It does not output the coordinates, and when changing it to print "black" once finding the color, there is still no output.

display video camera on two different windows tkinter python

i have a GUI python application that contain two different function windows and i'am trying to display video camera in the second window of tkinter application.
But, the video frame always be showing on the first windows!! i don't know if it is a problem of threads, or camera librairy. is there any fault in my code?
thanks in advance.
# add the necessairy librairy
import tkinter as tk
import threading
import time
import subprocess
from imutils.video import VideoStream
import time
import imutils
import cv2
import argparse
from PIL import Image
from PIL import ImageTk
class PhotoBoothApp:
def __init__(self,vs):
# store the video stream object and output path, then initialize
# the most recently read frame, thread for reading frames, and
# the thread stop event
self.vs = vs
self.frame = None
self.thread = None
self.stopEvent_2 = None
# initialize the root window and image panel
self.root = tk.Tk()
self.panel = None
self.user_input = tk.StringVar(self.root)
self.but_frame = tk.Frame(self.root)
w = 800 # width for the Tk root
h = 500 # height for the Tk root
# get screen width and height
ws = self.root.winfo_screenwidth() # width of the screen
hs = self.root.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk master window
x = (ws/3) - (w/3)
y = (hs/3) - (h/3)
# set the dimensions of the screen
# and where it is placed
self.root.geometry('%dx%d+%d+%d' % (w, h, x, y))
# when open the second window i want it to be on toplevel; it means when i click outside the frame it won't get hide
self.root.title("Test thread")
# create a button, that when pressed, will take the current
# frame and save it to file
btn = tk.Button(self.but_frame, bd = '5',text=" photo ")
self.but_frame.pack(side="left")
# start a thread that constantly pools the video sensor for
# the most recently read frame
self.stopEvent_2 = threading.Event()
self.thread = threading.Thread(target=self.videoLoop, args=())
self.thread.start()
# set a callback to handle when the window is closed
self.root.wm_protocol("WM_DELETE_WINDOW", self.onClose)
def videoLoop(self):
# DISCLAIMER:
# I'm not a GUI developer, nor do I even pretend to be. This
# try/except statement is a pretty ugly hack to get around
# a RunTime error that Tkinter throws due to threading
try:
# keep looping over frames until we are instructed to stop
while not self.stopEvent_2.is_set():
# grab the frame from the video stream and resize it to
# have a maximum width of 300 pixels
self.frame = self.vs.read()
self.frame = imutils.resize(self.frame, width=300)
#cv2.imshow('video player', self.frame)
# OpenCV represents images in BGR order; however PIL
# represents images in RGB order, so we need to swap
# the channels, then convert to PIL and ImageTk format
image = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)
image = Image.fromarray(image)
image = ImageTk.PhotoImage(image)
# if the panel is not None, we need to initialize it
if self.panel is None:
self.panel = tk.Label(image=image)
self.panel.image = image
self.panel.pack(side="left", padx=10, pady=10)
# otherwise, simply update the panel
else:
self.panel.configure(image=image)
self.panel.image = image
except RuntimeError :
print("[INFO] caught a RuntimeError_________________________________________")
def onClose(self):
# set the stop event, cleanup the camera, and allow the rest of
# the quit process to continue
print("[INFO] closing...")
self.stopEvent_2.set()
self.vs.stop()
self.root.quit()
# here's the main window
Mafenetre = tk.Tk()
#set main window title
Mafenetre.title("GUI")
Mafenetre['bg']='white' # couleur de fond
# get screen width and height
wf1= Mafenetre.winfo_screenwidth()
hf1= Mafenetre.winfo_screenheight()
A = str(wf1)
B = str(hf1)
# set the dimensions of the screen
# and where it is placed
w = 500 # width for the Tk root
h = 500 # height for the Tk root
# get screen width and height
ws = Mafenetre.winfo_screenwidth() # width of the screen
hs = Mafenetre.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk master window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
# set the dimensions of the screen
# and where it is placed
Mafenetre.geometry('%dx%d+%d+%d' % (w, h, x, y))
# add test button to the main window
reglage = tk.Button(Mafenetre, bd = '5',text=" PARAM ", bg='#c42034', fg='white',font= "Helvetica", 12))
reglage.pack(side=tk.TOP, padx=6,pady=35)
camera = VideoStream(0).start()
pba = PhotoBoothApp(camera)
Mafenetre.mainloop()

How to make python see a particular window?

I am using python 3, where i am using ImageGrab to see the portion of the screen.
The thing I want to do is get the full application window may be it of any size that is the program will get all the window that I want.
My Code Is:
from PIL import ImageGrab
import numpy as np
import cv2
while(True):
img = ImageGrab.grab(bbox=(0,0,580,580))
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
#frame = img_np
cv2.imshow("test", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
I am getting this:
But I want this.The Full Window:
I also want to grab the window anywhere on the screen.

How to save video Python 3.5

I have a video 256x300 uint8 (gray). I am tracking an object and displaying a rectangle in the video. I'd like to save that video (with the rectangle displayed). I tried some examples but they are all for RGB video.
import cv2
import sys
import subprocess as sp
if __name__ == '__main__' :
# Set up tracker.
# Instead of MIL, you can also use
# BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN
tracker = cv2.Tracker_create("MIL")
# Read video
video = cv2.VideoCapture("videotest.avi")
# Exit if video not opened.
if not video.isOpened():
print("Could not open video")
sys.exit()
# Read first frame.
ok, frame = video.read()
if not ok:
print("Cannot read video file")
sys.exit()
# Define an initial bounding box
bbox = (90, 10, 30, 30)
# Uncomment the line below to select a different bounding box
# bbox = cv2.selectROI(frame, False)
# Initialize tracker with first frame and bounding box
ok = tracker.init(frame, bbox)
while True:
# Read a new frame
ok, frame = video.read()
if not ok:
break
# Update tracker
ok, bbox = tracker.update(frame)
# Draw bounding box
if ok:
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (0,0,255))
# Display result
cv2.imshow("Tracking", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27 : break
video.release()
cv2.destroyAllWindows()

Plot mouse clicks over an image

I writing a code in Python 3 to plot some markers over a DICOM image. for this, I wrote a very short program:
In the main program, I read the DICOM filename from the terminal and plot the image.
main_prog.py:
import sys
import dicom as dcm
import numpy as np
from matplotlib import pyplot as plt
from dicomplot import dicomplot as dcmplot
filename = sys.argv[1]
dicomfile = dcm.read_file(filename)
dicomimg = dicomfile.pixel_array
fig = plt.figure(dpi = 300)
ax = fig.add_subplot(1, 1, 1)
plt.set_cmap(plt.gray())
plt.pcolormesh(np.flipud(dicomimg))
dcm = dcmplot(ax)
plt.show()
Then, I define a class to store the coordinates clicked by the user and plot each of them at a time over the image:
dicomplot.py
from matplotlib import pyplot as plt
class dicomplot():
def __init__(self, img):
self.img = img
self.fig = plt.figure(dpi = 300)
self.xcoord = list()
self.ycoord = list()
self.cid = img.figure.canvas.mpl_connect('button_press_event', self)
def __call__(self, event):
if event.button == 1:
self.xcoord.append(event.x)
self.ycoord.append(event.y)
self.img.plot(self.ycoord, self.xcoord, 'r*')
self.img.figure.canvas.draw()
elif event.button == 2:
self.img.figure.canvas.mpl_disconnect(self.cid)
elif event.button == 3:
self.xcoord.append(-1)
self.ycoord.append(-1)
The problem is that when I click over the image, the markers appear in a different scale, and not over the image as they are supposed to.
How can I modify my code so when I click on the image, all the mouse clicks are stored and ploted in the desired position?
The MouseEvent objects carry both a x/y andxdata/ydata attributes (docs). The first set is in screen coordinates (ex pixels from the lower left) and the second set (*data) are in the data coordinates.
You might also be interested in mpldatacursor.

Resources