Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cmd.py to new VisionClient #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions python/cmd.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
import asyncio
import asyncio,os

from viam.robot.client import RobotClient
from viam.rpc.dial import Credentials, DialOptions
from viam.services.vision import VisionServiceClient
from viam.services.vision import VisionClient
from viam.components.camera import Camera
import numpy as np
import cv2
import numpy as np

##on command line run:
##export ROBOT_SECRET=your_robot_secret from app.viam.com
##export ROBOT_ADDRESS=your_robot_address from app.viam.com

async def connect():
creds = Credentials(
type='robot-location-secret',
payload='[PUT IN INFO FROM ROBOT CONNECT TAB ON APP]')
payload=os.getenv('ROBOT_SECRET'))
opts = RobotClient.Options(
refresh_interval=0,
dial_options=DialOptions(credentials=creds)
)
return await RobotClient.at_address('[INFO FROM ROBOT CONNECT TAB ON APP]', opts)

return await RobotClient.at_address(os.getenv('ROBOT_ADDRESS'), opts)

async def main():
# establish a connection to the robot client

robot = await connect()
# grab camera from the robot
cam1 = Camera.from_robot(robot, "cam1")
# grab your vision service, here named "myDetector", which has the TFLite detector already registered
myDetector = VisionServiceClient.from_robot(robot, "myDetector")
# make a display window
cam1 = Camera.from_robot(robot, "web_cam")
# grab Viam's vision service for the detector
my_detector = VisionClient.from_robot(robot, "vision_svc")

cv2.namedWindow('object_detect', cv2.WINDOW_NORMAL)
# loop forever: find objects using Viam Vision, and draw boxes aroung them using openCV
while(True):
img = await cam1.get_image() # default is JPEG
detections = await myDetector.get_detections(img)
# turn image into numpy array
pix = np.array(img, dtype=np.uint8)
pix = cv2.cvtColor(pix, cv2.COLOR_RGB2BGR)
# put boxes around objects with enough confidence (> 0.6)
conf = 0.6
for d in detections:
if d.confidence > conf:
cv2.rectangle(pix, (d.x_min, d.y_min), (d.x_max, d.y_max), (0, 0, 255), 3)
cv2.imshow('object_detect',pix)
cv2.waitKey(1)

await robot.close()
try:
while(True):
print("hi")
img = await cam1.get_image()
detections = await my_detector.get_detections(img)
print(detections)
# turn image into numpy array
pix = np.array(img, dtype=np.uint8)
pix = cv2.cvtColor(pix, cv2.COLOR_RGB2BGR)
# put boxes around objects with enough confidence (> 0.6)
conf = 0.6
for d in detections:
if d.confidence > conf:
cv2.rectangle(pix, (d.x_min, d.y_min), (d.x_max, d.y_max), (0, 0, 255), 3)
cv2.imshow('object_detect',pix)
cv2.waitKey(1)
finally:
print("closing robot")
await robot.close()
print("robot closed")


if __name__ == '__main__':
asyncio.run(main())