Create a gist now

Instantly share code, notes, and snippets.

@dataverger /README
Last active Jul 26, 2017

What would you like to do?
Testing Mods to PythonForEngineers Book Source
I uploaded files modified from Shantnu Tiwari's Python For Engineers (PyEng) respository.
Specifically, the Image_Video subdirectory has sample programs, which I modified to use matplotlib.pyplot's imshow() insead of OpenCV's imshow().
This is not ideal, but got the programs further along in the absense of a working OpenCV on Ubuntu.
The next step was to avoid the GUI altogether and use OpenCV's imwrite() to save images to a file. The pyplot examples are left in, but commented out for reference.
import cv2
import sys
import numpy as np
import matplotlib.pyplot as plt
# The first argument is the image
image = cv2.imread(sys.argv[1])
#conver to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#blur it
blurred_image = cv2.GaussianBlur(image, (7,7), 0)
#
# Show all 3 images
#
# Here are some alternatives if displaying with cv2 isn't working
#
#cv2.imshow("Original Image", image)
#cv2.imshow("Gray Image", gray_image)
#cv2.imshow("Blurred Image", blurred_image)
#
# 1) Instead of showing the image with cv2, we will save it
cv2.imwrite('blur2_original.png', image)
cv2.imwrite('blur2_gray.png', gray_image)
cv2.imwrite('blur2_blurred.png', blurred_image)
# 2) Use pyplot to show the image. Just uncomment these lines:
#
#plt.imshow(image) # "Original Image" )
#plt.show()
#plt.imshow(gray_image) # "Gray Image" )
#plt.show()
#plt.imshow(blurred_image) # "Blurred Image" )
#plt.show()
# Uncomment this if showing via GUI
#cv2.waitKey(0)
import cv2
import sys
import numpy as np
import matplotlib.pyplot as plt
# Read the image
image = cv2.imread("cards.jpg")
#convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#blur it
blurred_image = cv2.GaussianBlur(gray_image, (7,7), 0)
# Show both our images
#
# Here are some alternatives if displaying with cv2 isn't working
#
#cv2.imshow("Original image", image)
#cv2.imshow("Blurred image", blurred_image)
#
# 1) Instead of showing the image with cv2, we will save it
cv2.imwrite('count_cards2_original.png', image)
cv2.imwrite('count_cards2_blurred.png', blurred_image)
# 2) Use pyplot to show the image. Just uncomment these lines:
#
#plt.imshow(image)
#plt.show()
#plt.imshow(blurred_image)
#plt.show()
# Run the Canny edge detector
canny = cv2.Canny(blurred_image, 30, 100)
#
#cv2.imshow("Canny", canny)
#
cv2.imwrite('count_cards2_canny.png', canny)
#
# OR
#
#plt.imshow(canny)
#plt.show()
im, contours, hierarchy= cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print("Number of objects found = ", len(contours))
cv2.drawContours(image, contours, -1, (0,255,0), 2)
#
#cv2.imshow("objects Found", image)
#
cv2.imwrite('count_cards2_objects.png', image)
#
# OR
#
#plt.imshow(image)
#plt.show()
# Uncomment this if showing via GUI
#cv2.waitKey(0)
#plt.clf()
import cv2
import sys
import numpy as np
import matplotlib.pyplot as plt
# Read the image. The first command line argument is the image
image = cv2.imread(sys.argv[1])
# Here are some alternatives if displaying with cv2 isn't working
#
#cv2.imshow("Image", image)
#
# 1) Instead of showing the image with cv2, we will save it
cv2.imwrite('display2.png', image)
# 2) Use pyplot to show the image. Just uncomment these lines:
#
#plt.imshow(image)
#plt.show()
# Uncomment this if showing via GUI
#cv2.waitKey(0)
import cv2
import sys
import numpy as np
import matplotlib.pyplot as plt
# The first argument is the image
image = cv2.imread(sys.argv[1])
#convert to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#blur it
blurred_image = cv2.GaussianBlur(gray_image, (7,7), 0)
# Here are some alternatives if displaying with cv2 isn't working
#
#cv2.imshow("Original Image", image)
#
# 1) Instead of showing the image with cv2, we will save it
cv2.imwrite('edge_detect2_original.png', image)
# 2) Use pyplot to show the image. Just uncomment these lines:
#
#plt.imshow(image)
#plt.show()
# Use low thresholds
canny = cv2.Canny(blurred_image, 10, 30)
#
#cv2.imshow("Canny with low thresholds", canny)
#
cv2.imwrite('edge_detect2_canny.png', canny)
#
# OR
#
#plt.imshow(canny)
#plt.show()
# Use high thresholds
canny2 = cv2.Canny(blurred_image, 50, 150)
#
#cv2.imshow("Canny with high thresholds", canny2)
#
cv2.imwrite('edge_detect2_canny2.png', canny2)
#
# OR
#
#plt.imshow(canny2)
#plt.show()
# Uncomment this if showing via GUI
#cv2.waitKey(0)
#!/usr/bin/python
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
imgpath = sys.argv[1]
cascasdepath = "haarcascade_frontalface_default.xml"
image = cv2.imread(imgpath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier(cascasdepath)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 5,
minSize = (30,30)
)
print("The number of faces found = ", len(faces))
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+h, y+h), (0, 255, 0), 2)
# Here are some alternatives if displaying with cv2 isn't working
#
#cv2.imshow("Faces found", image)
#
# 1) Instead of showing the image with cv2, we will save it
cv2.imwrite('face_detect2.png', image)
# 2) Use pyplot to show the image. Just uncomment these lines:
#
#plt.imshow(image)
#plt.show()
# Uncomment this if showing via GUI
#cv2.waitKey(0)
#!/usr/bin/python
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt
cascasdepath = "haarcascade_frontalface_default.xml"
if len(sys.argv) < 2:
video_capture = cv2.VideoCapture(0)
else:
video_capture = cv2.VideoCapture(sys.argv[1])
while True:
ret, image = video_capture.read()
if not ret:
break
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier(cascasdepath)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 5,
minSize = (30,30)
)
#print("The number of faces found = ", len(faces))
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+h, y+h), (0, 255, 0), 2)
# Here are some alternatives if displaying with cv2 isn't working
#
#cv2.imshow("Faces found", image)
#
# 1) Instead of showing the image with cv2, we will save it
cv2.imwrite('webcam_face_detect2.png', image)
# 2) Use pyplot to show the image. Just uncomment these lines:
#
#plt.imshow(image)
#plt.show()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
# cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment