Unable to get the real time data out from MATLAB Function block. I am calling python code in it.

2 visualizaciones (últimos 30 días)
Hello Sir/Mam
Please help me...
I want to control ball on the square plate pivoted at its center. For that I want real time co-ordinates of ball. I am calling python program in Matlab function block of Simulink. I am getting co-ordinates in Diagonosis section but unable to get cordinates out from Matlab Function Block.
Here is my python code
import cv2
import numpy as np
import serial
def Detection(frame):
frame = frame[0:480, 80:560]
frame = cv2.GaussianBlur(frame, (15,15), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_orange = np.array([0,0,192])
upper_orange = np.array([360,32,255])
mask = cv2.inRange(hsv, lower_orange, upper_orange)
kernel = np.ones((3,3), np.uint8)
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)
contours= cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
if len(contours)>0:
max_contour = max(contours, key=cv2.contourArea)
rectangle = cv2.boundingRect(max_contour)
x,y,w,h = rectangle
rectangle_pts = np.array([[x,y],[x+w,y],[x+w,y+h],[x,y+h]], np.int32)
cv2.polylines(frame, [rectangle_pts], True, (0,255,0), 3)
X = x+w//2
Y = y+h//2
center_pt=(X,Y)
cv2.circle(frame, center_pt, 8, (0,0,255), -1)
else:
X = 10000
Y = 10000
x = 0;
y = 0;
w = 240;
h = 240;
rectangle_pts = np.array([[x,y],[x+w,y],[x+w,y+h],[x,y+h]], np.int32)
cv2.polylines(frame, [rectangle_pts], True, (0,255,0), 2)
x2 = 240;
y2 = 240;
rectangle_pts2 = np.array([[x2,y2],[x2+w,y2],[x2+w,y2+h],[x2,y2+h]], np.int32)
cv2.polylines(frame, [rectangle_pts2], True, (0,255,0), 2)
cv2.imshow('frame',frame)
cv2.imshow('mask', mask)
return X,Y
def camCapture(url):
ser = serial.Serial()
ser.baudrate = 115200
ser.port = 'COM3'
ser.open()
data = '#'+str('240')+'$'+str('240')+'\r\n'
ser.write(data.encode())
url="http://192.168.43.1:8080/video"
cap = cv2.VideoCapture(url)
while(cap.isOpened()):
_, frame = cap.read()
X, Y = Detection(frame)
if(X<10000):
P = str(X)
Q = str(Y)
print('P= ')
print(P)
print('Q =')
print(Q)
print(data)
ser.write(P.encode())
ser.write(Q.encode())
k = cv2.waitKey(5) & 0xFF
if k==27:
break
ser.close()
cv2.destroyAllWindows()
cap.release()
camCapture("")
And Matlab fuction block code is :
function [P,Q]= RunPython(url)
P=0;
Q=0;
coder.extrinsic('py.camera_Virag.camCapture')
P,Q= py.camera_Virag.camCapture(url);
end
Here is my Simulink Block Diagram
Thank You

Respuestas (1)

Shree Charan
Shree Charan el 5 de Abr. de 2023
In the python code, the values of P and Q are written to the serial port 'COM3' and are not returned from the camCapture function.
Consider reading the data from the serial port 'COM3' in MATLAB.
You may refer to this documentation for the same. Read data from serial port - MATLAB read (mathworks.com)
  1 comentario
JAIN VIRAG
JAIN VIRAG el 5 de Abr. de 2023
Thank You Sir for your reply,
Problem remains the same, not getting output in scope (Simulink) also in workspace (MATLAB).
How can I transfer the data from python to Matlab in other way ?
Please Help

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by