I`m trying to built a GUI using pyqt5. In the GUI I need to stream a video from the raspberry pi camera via WiFi. And I need to send game-pad inputs to raspberry pi. my code as follows:
import sys,time,math
import cv2
import numpy as np
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5 import uic
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QImage,QPixmap
from PyQt5.QtWidgets import QDialog,QApplication
import urllib.request
import threading
import paho.mqtt.client as mqtt
import inputs
m1="0"
broker_ip="computerIP"
url="http://raspberrypiIP/html/cam_pic.php"
#this function get raspberry pi camera video to laptop via WiFi
def url_to_image(url):
with urllib.request.urlopen(url) as resp:
image = np.asarray(bytearray(resp.read()),dtype ="uint8")
image = cv2.imdecode(image,cv2.IMREAD_COLOR)
return image
def on_connect(client, userdata, flags, rc):
client.subscribe("arm")
def on_message(client, userdata, msg):
print (msg.payload)
#mqtt is used to send data from laptop to raspberrypi
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_ip, 1883, 60)
threading.Thread(target=client.loop_forever).start()
def Stop1():
m1="0,0,0,0"
client.publish("arm",str(m1))
def Forward1():
m1="0,0,100,100"
client.publish("arm",str(m1))
def Reverse1():
m1="0,0,-100,-100"
client.publish("arm",str(m1))
def cnsl(): # this function detect the gamepad input
while True:
events=inputs.get_gamepad()
for event in events:
if event.code=="ABS_Y" and event.state>1000:
Forward1()
elif event.code=="ABS_Y" and event.state<-1000:
Reverse1()
elif event.code=="ABS_Y" and event.state==128:
Stop1()
class test1(QtWidgets.QMainWindow):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
uic.loadUi("test1.ui",self)
def slot1(self): # this function runs when Pushbutton is clicked
threadVideo=threading.Thread(target=self.onClicked())
threadConsol=threading.Thread(target=self.cnsl())
threadVideo.start()
threadConsol.start()
threadVideo.join()
threadConsol.join()
@pyqtSlot()
# this function display the raspberrypi video in the buited GUI
def onClicked(self):
print ("video")
while (True):
img=url_to_image(url)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
qformat=QImage.Format_Indexed8
if len(img.shape)==3:
if (img.shape[2])==4:
qformat=QImage.Format_RGBA888
else:
qformat=QImage.Format_RGB888
img=QImage(img,img.shape[1],img.shape[0],qformat)
img=img.rgbSwapped()
self.label_3.setPixmap(QPixmap.fromImage(img))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
app=QtWidgets.QApplication(sys.argv)
window=test1()
window.show()
app.exec_()
When I run this code, video stream display in the GUI. But I cannot send game-pad inputs. I have tried with 2 buttons also. But both buttons did not work simultaneously. Is any one have any idea??