<jupyter_start><jupyter_text>Machine Learning for Engineers: [Computer Vision Introduction](https://www.apmonitor.com/pds/index.php/Main/ComputerVisionIntro)- [Computer Vision Introduction](https://www.apmonitor.com/pds/index.php/Main/ComputerVisionIntro) - Source Blocks: 13 - Description: Computer vision is how computers automate tasks that mimic human response to visual information. Computers gain high-level understanding and take actions from digital images or videos.- [Course Overview](https://apmonitor.com/pds)- [Course Schedule](https://apmonitor.com/pds/index.php/Main/CourseSchedule)<jupyter_code>pip install opencv-python
import urllib.request

# download image
url = 'http://apmonitor.com/pds/uploads/Main/students_walking.jpg'
urllib.request.urlretrieve(url, 'students_walking.jpg')
import cv2 as cv
im = cv.imread('students_walking.jpg')
h,w,c = im.shape
im1 = cv.resize(im,(300,200))
im2 = cv.resize(im,None,fx=0.5,fy=0.5)
width  = 300      # new width
h,w,c  = im.shape # get image size
scale  = width/w  # scaling factor
height = int(h * scale)
dim    = (width, height)
im3    = cv.resize(im,dim)
import matplotlib.pyplot as plt
im2 = plt.imread('students_walking.jpg')
plt.imshow(im2)
plt.imshow(im[:,:,[2,1,0]])
name='image'
cv.imshow(name,im)
cv.namedWindow(name, cv.WINDOW_AUTOSIZE)
cv.waitKey(0) # waits for key press
cv.destroyAllWindows() 
import cv2 as cv
import matplotlib.pyplot as plt
im = cv.imread('students_walking.jpg')
im2 = plt.imread('students_walking.jpg')
plt.subplot(1,2,1)
plt.imshow(im)
plt.title('BGR (OpenCV)')
plt.subplot(1,2,2)
plt.imshow(im2)
plt.title('RGB (Matplotlib)')
cv.imwrite('students.png',im)
import cv2 as cv
import time

# Get camera Object
camera = cv.VideoCapture(0)                         
w = int(camera.get(cv.CAP_PROP_FRAME_WIDTH))    
h = int(camera.get(cv.CAP_PROP_FRAME_HEIGHT))

# Write video.avi
out = cv.VideoWriter('video.avi', \
                     cv.VideoWriter_fourcc(*'XVID'), \
                     25, (w,h))

# Create Window to video frames
WindowName = 'View'
cv.namedWindow(WindowName, cv.WINDOW_AUTOSIZE)

# Save and view 5 second video
start = time.time()
while time.time()-start<=5.0:
    ret0, frame = camera.read()         
    cv.imshow(WindowName, frame)        
    out.write(frame)                    
    if cv.waitKey(1) & 0xFF == ord('q'):
        break
cv.imwrite('frame.jpg', frame)          

# Release camera and video file
camera.release(); out.release()
cv.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13824701195219125, 'fraction_numerical': 0.01752988047808765, 'mean_word_length': 4.022, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '47012', 'license': 'MIT', 'n_tokens_mistral': 912, 'n_tokens_neox': 880, 'n_words': 188, 'path': 'Computer_Vision.ipynb', 'repo_name': 'the-mahapurush/pds', 'size': '2510'}