control pan tilt and zoom camera using python - python-3.x

I've been tried control PTZ camera pro 2, but the video stream and pan tilt still can't work together. Maybe someone can solve this issue. Feedback and suggestion are welcome.
This is complete code using python language for control PTZ Camera.
https://github.com/deviraanggi/Logitech-PTZ-Pro-2-Control
python3
from ctypes import *
import time
import ctypes
import clr
import os
import sys
import cv2
import PTZ
import clr
clr.AddReference('cobabikindll')
import PTZ
from PTZ import PTZDevicess, PTZType
device = PTZDevicess.GetDevice("PTZ Pro 2", PTZType.Absolute);
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
#device.Move(-1, 1)
device.MoveInternal(30,35)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Related

cv2.setMouseCallback() is not working in Colab

Below is my code:
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
prevX,prevY=-1,-1
def printCoordinate(event, x, y, flags, params):
global prevX,prevY
if event==cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),3,(255,255,255),-1)
strXY='('+str(x)+','+str(y)+')'
font=cv2.FONT_HERSHEY_PLAIN
cv2.putText(img,strXY,(x+10,y-10),font,1,(255,255,255))
if prevX==-1 and prevY==-1:
prevX,prevY=x,y
else:
cv2.line(img,(prevX,prevY),(x,y),(0,0,255),5)
prevX,prevY=-1,-1
cv2_imshow(img)
img = np.zeros((800,800,3),dtype=np.uint8)
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
cv2_imshow(img)
cv2.setMouseCallback('image',printCoordinate)
cv2.waitKey()
cv2.destroyAllWindows()
I am trying to draw line or polygon with mouse clicks on an image and need to coordinates of the points of line and polygon.
But the code crashes when it reaches line "cv2.setMouseCallback('image',printCoordinate)". Can anyone please guide me how can I fix it on colab?
PS: On my local PC the code works fine but it does not work on Colab.
I have replaced the cv2.imshow to cv2_imshow as .imshow is not supported by colab.

How to show episode in rendered openAI gym environment

If we look at the previews of the environments, they show the episodes increasing in the animation on the bottom right corner. https://gym.openai.com/envs/CartPole-v1/ .Is there a command to explicitly show that?
I don't think there is a command to do that directly available in OpenAI, but I've written some code that you can probably adapt to your purposes. This is the end result:
These is how I achieve the end result:
For each step, you obtain the frame with env.render(mode='rgb_array')
You convert the frame (which is a numpy array) into a PIL image
You write the episode name on top of the PIL image using utilities from PIL.ImageDraw (see the function _label_with_episode_number in the code snippet).
You save the labeled image into a list of frames.
You render the list of frames as a GIF using matplotlib utilities.
Here is the code I wrote for obtaining a GIF of the behavior of a random agent with the Episode number displayed in the top left corner of each frame:
import os
import imageio
import numpy as np
from PIL import Image
import PIL.ImageDraw as ImageDraw
import matplotlib.pyplot as plt
def _label_with_episode_number(frame, episode_num):
im = Image.fromarray(frame)
drawer = ImageDraw.Draw(im)
if np.mean(im) < 128:
text_color = (255,255,255)
else:
text_color = (0,0,0)
drawer.text((im.size[0]/20,im.size[1]/18), f'Episode: {episode_num+1}', fill=text_color)
return im
def save_random_agent_gif(env):
frames = []
for i in range(5):
state = env.reset()
for t in range(500):
action = env.action_space.sample()
frame = env.render(mode='rgb_array')
frames.append(_label_with_episode_number(frame, episode_num=i))
state, _, done, _ = env.step(action)
if done:
break
env.close()
imageio.mimwrite(os.path.join('./videos/', 'random_agent.gif'), frames, fps=60)
env = gym.make('CartPole-v1')
save_random_agent_gif(env)
You can find a working version of the code here: https://github.com/RishabhMalviya/dqn_experiments/blob/master/train_and_visualize.py#L10

In opencv imshow() function does not open the new window and display the image in jupyter notebook

import cv2
cap = cv2.VideoCapture(0)
status , photo = cap.read()
cv2.imwrite('Surendar.png',photo)
cap.release()
cv2.imshow('image', photo)
cv2.waitKey(5000)
cv2.destroyAllWindows()
I interpreted this code in my jupyter notebook. It just complies but does not show the new window of picture.
Try changing cv2.waitKey(5000) to cv2.waitKey(0) so the window will stay up until the user closes it. It looks like the window was waiting for 5000 milliseconds before it would destroy the window.
EDIT
Instead of using cv2 to display your image, try using matplot instead
import cv2
from matplotlib import pyplot as plt
cap = cv2.VideoCapture(0)
status , photo = cap.read()
cv2.imwrite('Surendar.png',photo)
cap.release()
plt.imshow(photo)
plt.show()

Python 3 neopixel non-functional

import neopixel
import board
import sounddevice as sd
from numpy import linalg as LA
import numpy as np
def getvolume(indata, outdata, frames, time, status):
volume_norm = np.linalg.norm(indata)*10
with sd.Stream(callback=getvolume):
sd.sleep(250)
pixels = neopixel.NeoPixel(board.D18, 144)
pixels.fill((volume_norm, volume_norm, volume_norm))`
I want the code to make the LED strip shine brighter the louder the sound is. It currently doesn't do anything. What am I doing wrong?

Live Screen Monitoring With Python3 Pytesseract

I am working on a python3 project on windows 10, and I was wondering if anyone knew of anyway to pass an opencv screen grab through pytesseract? If not, is there any other OCR that you could?
Here is the code for the opencv screen grab:
import numpy as np
from PIL import ImageGrab
import cv2
while True:
screen = np.array(ImageGrab.grab(bbox=(0,40,800,640)))
cv2.imshow('window', cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
I know very little about pytesseract, but this might get you started:
#!/usr/bin/env python3
import numpy as np
from PIL import ImageGrab
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
from textblob import TextBlob
# Grab some screen
screen = ImageGrab.grab(bbox=(0,0,800,640))
# Make greyscale
w = screen.convert('L')
# Save so we can see what we grabbed
w.save('grabbed.png')
text = pytesseract.image_to_string(w)
correctedText = TextBlob(text).correct()
print(correctedText)
From this grab:
I got:
# Terminal Shell Edit View Window Help
The writing is on the wall

Resources