Unable to get the real time data out from MATLAB Function block. I am calling python code in it.
2 ビュー (過去 30 日間)
古いコメントを表示
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1345744/image.png)
Thank You
0 件のコメント
回答 (1 件)
Shree Charan
2023 年 4 月 5 日
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)
参考
カテゴリ
Help Center および File Exchange で Call Python from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!