Code that runs on pc doesnt runs on RPi3B+

My code is working on the robot when I plugged the camera to the pc and robot is just working flawless but when I upload the same code into the RPi3B+ code is outputting same result

here is my code that positioning the robot to contours center

import cv2
import logging
import numpy as np
from networktables import NetworkTables

CAMERA CONF STUFF

kam = cv2.VideoCapture(1)
kam.set(cv2.CAP_PROP_FRAME_WIDTH, 180)
kam.set(cv2.CAP_PROP_FRAME_HEIGHT, 180)
kam.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0)
kam.set(cv2.CAP_PROP_EXPOSURE, -15.0)

NETWORKING STUFF

logging.basicConfig(level=logging.DEBUG)
ip = “10.60.38.2”
NetworkTables.initialize(server=ip)
table = NetworkTables.getTable(“idris_ustam”)

HYPER PARAMETERS

lower = np.array([65, 100, 100])
upper = np.array([85, 255, 255])
x = 0
y = 0
w = 0
h = 0
cY = 0
cX = 0
ekranY = 0
ekranX = 0
alan = 0

while True:
_, ret = kam.read()
hsv = cv2.cvtColor(ret, cv2.COLOR_BGR2HSV)

elonmask = cv2.inRange(hsv, lower, upper)

_, contours, _ = cv2.findContours(elonmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

if len(contours) > 0:

    for c in contours:
        x, y, w, h = cv2.boundingRect(c)

        cY = y + (h / 2)
        cX = x + (w / 2)
        cY = int(cY)
        cX = int(cX)

        alan = w * h


else:
    cX = 0



table.putNumber("cX", cX)
table.putNumber("ALAN", alan)

print(cX)

A couple questions.

  1. What error message are you getting?
  2. Are the OpenCV versions on the Pi and PC the same?

You may also wat to try changing
“kam = cv2.VideoCapture(1)” to “kam = cv2.VideoCapture(0)”.

1 Like

I changed videoCapture and the error is Pi sending same cursed value of 88

Have you connected a monitor to the Pi, or used VNC to see if the Pi is actually running the code without errors?

Can you please throw some documentetion about VNC and how to see what is going on Pi with Raspbian Lite

You would need to install the desktop GUI and then enable VNC through raspi-config.

sudo apt-get install --no-install-recommends xserver-xorg
sudo apt-get install raspberrypi-ui-mods
sudo apt-get install lightdm

sudo apt-get update
sudo apt-get upgrade

reboot

Once reboot is complete, SSH and type raspbi-config.
Services => enable vnc

As for your issue, it may be the way cameras a handled with Linux. Is your computer windows? Make sure if you go to /dev/camera0 or something along those lines there is something related to a camera there.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.