migparra73 / servohandJetson

Jetson Implementation of the Servohand Project
MIT License
0 stars 1 forks source link

Create video stream in OpenCV using webcam #1

Open migparra73 opened 1 year ago

migparra73 commented 1 year ago

Create a video stream in Python using OpenCV (cv2) Make it such that it can be used for a custom Gym environment (TBD what this is...)

perezcap commented 1 year ago

Hi! So this is what I've done for this task. Could you please check it out? Thanks! :)

Ps. It didn't let me upload .py files :(

import cv2
import gym
import numpy as np

class CustomCameraEnv(gym.Env):
    def __init__(self):
        super(CustomCameraEnv, self).__init__()
        self.cap = cv2.VideoCapture(0)
        self.action_space = gym.spaces.Discrete(2)  # Define the action space here
        self.observation_space = gym.spaces.Box(low=0, high=255, shape=(480, 640, 3), dtype=np.uint8)

    def reset(self):
        # Reset the camera and return the initial observation
        return self._get_observation()

    def step(self, action):
        # Update the observation, reward, done, and info based on the action

        # Example:
        observation = self._get_observation()
        reward = 0.0
        done = False
        info = {}

        if action == 1:
            reward += 1.0  # Example reward increment for a specific action

        return observation, reward, done, info

    def render(self, mode='human'):
        # Display the current frame
        ret, frame = self.cap.read()
        if mode == 'human':
            cv2.imshow('Video Stream | Servo Hand Project', frame)
            cv2.waitKey(1)

    def close(self):
        # Release resources
        self.cap.release()
        cv2.destroyAllWindows()

    def _get_observation(self):
        ret, frame = self.cap.read()
        return frame

env = CustomCameraEnv()
obs = env.reset()

for _ in range(1000):  # Replace 1000 with the desired number of steps
    env.render()
    action = env.action_space.sample()  # Replace with the action selection logic
    obs, reward, done, _ = env.step(action)
    if done:
        break

    key = cv2.waitKey(1)
    if key == ord('q'):
        break

env.close()