Installing Python 3.6 on Ubuntu

Since I have been getting a lot of questions about installing Python 3.6 on Ubuntu, I thought I could put together a document for quick reference. The default version of Python on Ubuntu (until 17.04) is Python 3.5. To download and install Python 3.6, follow the commands given below based on your version of Ubuntu.

Ubuntu 14.04 (Trusty), 16.04 (Xenial)

If you are using Ubuntu 14.04 or 16.04, you can use Felix Krull’s deadsnakes PPA at https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa:

Command 1:

sudo add-apt-repository ppa:deadsnakes/ppa

ubuntu_img1

Command 2:

sudo apt-get update

ubuntu_img2

Command 3:

sudo apt-get install python3.6

ubuntu_img3

Alternatively, you can use J Fernyhough’s PPA at https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6:

sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

Ubuntu 16.10, 17.04

If you are using Ubuntu 16.10 or 17.04, then Python 3.6 is in the universe repository, so you can just run:

sudo apt-get update
sudo apt-get install python3.6

After installation for Ubuntu 14.04, 16.04, 16.10 and 17.04

To invoke the Python 3.6 interpreter, run python3.6.

Ubuntu 17.10 and 18.04 (Bionic)

Ubuntu 17.10 and 18.04 already come with Python 3.6 as default. Just run python3 to invoke it.

Advertisements

Color tracking using webcam, OpenCV and Python

In this post, a simple method to obtain continuous video from a standard webcam is described. It also provides code to track a certain color from the camera feed. For this purpose, we make use of OpenCV functions in python.

To test if you are able to capture the live video feed, use the code snippet provided below.

# import the necessary packages
import cv2
#Declare variables for window
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
# try to get the first frame
if vc.isOpened():
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # Exit when ESC key is pressed
break
vc.release() #To unlock camera on windows OS
cv2.destroyWindow("preview")

If you are unable to close the window, simply press ‘Esc’. It has been assigned as the exit key in the code.

Now, to verify color tracking using HSV values, use the following code:

#Import necesessary packages
import cv2
import numpy as np
#Declare variables for window
vc = cv2.VideoCapture(0)
while(1):
# Take each frame
ret, frame = cap.read()
if(ret):
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([50, 100, 100], dtype=np.uint8)#[110,50,50]
upper_blue = np.array([70,255,255], dtype=np.uint8)#[130,255,255]
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27: #Exit when ESC key is pressed
break
vc.release()
cv2.destroyAllWindows()

If you would like to determine HSV values for any color, please use the example below:

>> green = np.uint8([[[0,255,0]]])
>> hsv_green = cv2.cvtColor(green,cv2.COLOR_BGR2HSV)
>> print hsv_green

Now the value obtained (here, [[[60 255 255]]]) offers the base to set the range of colors to determine. Use [H-10, 100, 100] as lower bound and [H+10, 255, 255] as upper bound.

Hope this helps.