Categories
OpenCV Vision

Installing OpenCV on computer

OpenCV will be needed, for the robot to make sense of the camera input. We’ll have the camera on a raspberry pi of some sort.

pip install opencv-contrib-python

Someone made a useful website on OpenCV here https://www.pyimagesearch.com/

root@chrx:/opt/imagezmq/tests# python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'cv2.cv2' has no attribute '__version'
>>> cv2.__version__
'4.2.0'

Just gotta make sure it uses python3, not python2.

We’ll run it on the raspberry pi (https://www.pyimagesearch.com/2018/09/19/pip-install-opencv/) eventually, but for now, just testing core features on GalliumOS. It’s also required by imagezmq (https://github.com/jeffbass/imagezmq)

The robot has to use its sensors to find chickens, eggs, and decide whether to walk or turn. Stimuli to trigger actions.

pip install zmq

Might also need ‘pip install imutils’

Copying imagezmq.py to the tests folder cause I don’t know shit about python and how to import it from the other folder. So here’s the server:

python3 test_1_receive_images.py

import sys
import cv2
import imagezmq

image_hub = imagezmq.ImageHub()
while True:  # press Ctrl-C to stop image display program
    image_name, image = image_hub.recv_image()
    cv2.imshow(image_name, image)
    cv2.waitKey(1)  # wait until a key is pressed
    image_hub.send_reply(b'OK')

And the client program:

python3 test_1_send_images.py


import sys
import time
import numpy as np
import cv2
import imagezmq

# Create 2 different test images to send
# A green square on a black background
# A red square on a black background

sender = imagezmq.ImageSender()
i = 0
image_window_name = 'From Sender'
while True:  # press Ctrl-C to stop image sending program
    # Increment a counter and print it's value to console
    i = i + 1
    print('Sending ' + str(i))

    # Create a simple image
    image = np.zeros((400, 400, 3), dtype='uint8')
    green = (0, 255, 0)
    cv2.rectangle(image, (50, 50), (300, 300), green, 5)

    # Add counter value to the image and send it to the queue
    cv2.putText(image, str(i), (100, 150), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 255), 4)
    sender.send_image(image_window_name, image)
    time.sleep(1)

Cool it sent pics to my screen. Heh “Hershey Simplex” sounds more like a virus than a font.