Posts

Showing posts from November, 2022
Image
  Computer vision:Drowsiness Detection using YOLOv5   #!pip install torch==1.12.1+cpu torchvision==0.13.1+cpu torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cpu #!git clone https://github.com/ultralytics/yolov5 #!cd yolov5 & pip install -r requirements.txt import os  os.chdir(r"C:\Users\amb\Downloads\drownsenesyolov5") import torch from matplotlib import pyplot as plt import numpy as np import cv2 model = torch.hub.load('ultralytics/yolov5', 'yolov5s') img = 'https://resources.stuff.co.nz/content/dam/images/1/k/q/r/d/c/image.related.StuffLandscapeSixteenByNine.1420x800.1kseqt.png/1501707394942.jpg' results = model(img) results.print() results.render() %matplotlib inline  plt.imshow(np.squeeze(results.render())) plt.show() results.show() results.xyxy np.squeeze(results.render()).shape cap = cv2.VideoCapture(0) while cap.isOpened():     ret, frame = cap.read()          # Make detections      r...
Image
  Computer vision: Motion Detection and Tracking Using Opencv  import cv2 import numpy as np cap = cv2.VideoCapture('vtest.avi') frame_width = int( cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height =int( cap.get( cv2.CAP_PROP_FRAME_HEIGHT)) fourcc = cv2.VideoWriter_fourcc('X','V','I','D') out = cv2.VideoWriter("output.avi", fourcc, 5.0, (1280,720)) ret, frame1 = cap.read()# 1st frame ret, frame2 = cap.read() #2nd frame print(frame1.shape) while cap.isOpened():     diff = cv2.absdiff(frame1, frame2)             gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)             blur = cv2.GaussianBlur(gray, (5,5), 0)             _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)             dilated = cv2.dilate(thresh, None, iterations=3)             contours, _ = cv2.findContours(dilated, cv2.RETR_...
Image
  Computer vision course: find and draw contours in open CV  import numpy as np import cv2 img = cv2.imread('baseball.png') imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(imgray, 127, 255, 0) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) print("Number of contours = " + str(len(contours))) print(contours[0]) cv2.drawContours(img, contours, -1, (0, 255, 0), 3) cv2.drawContours(imgray, contours, -1, (0, 255, 0), 3) cv2.imshow('Image', img) cv2.imshow('Image GRAY', imgray) cv2.waitKey(0) cv2.destroyAllWindows()#close window #===================================# if you faced any issue contact me via  what's app :  +201210894349  or  facebook
Image
  Computer Vision Course: Image Blending using Pyramids in open CV  import cv2 import numpy as np apple = cv2.imread('apple.jpg') orange = cv2.imread('orange.jpg') print(apple.shape) print(orange.shape) apple_orange = np.hstack((apple[:, :256], orange[:, 256:])) # generate Gaussian pyramid for apple apple_copy = apple.copy() gp_apple = [apple_copy] for i in range(6):     apple_copy = cv2.pyrDown(apple_copy)            gp_apple.append(apple_copy)        # generate Gaussian pyramid for orange orange_copy = orange.copy() gp_orange = [orange_copy] for i in range(6):     orange_copy = cv2.pyrDown(orange_copy)           gp_orange.append(orange_copy)       # generate Laplacian Pyramid for apple apple_copy = gp_apple[5] lp_apple = [apple_copy] for i in range(5, 0, -1):     gaussian_expanded = cv2.pyrUp(gp_apple[i])           la...
Image
  Computer Vision Course: image pyramids (Gaussian pyramid and Laplacian pyramid)   import cv2 import numpy as np img = cv2.imread("lena.jpg") layer = img.copy() gaussian_pyramid_list = [layer] for i in range(6):     layer = cv2.pyrDown(layer)          gaussian_pyramid_list.append(layer)          #cv2.imshow(str(i), layer)      layer = gaussian_pyramid_list[5] cv2.imshow('upper level Gaussian Pyramid', layer) laplacian_pyramid_list = [layer] for i in range(5, 0, -1):     gaussian_extended = cv2.pyrUp(gaussian_pyramid_list[i])          laplacian = cv2.subtract(gaussian_pyramid_list[i-1], gaussian_extended)          cv2.imshow(str(i), laplacian)      cv2.imshow("Original image", img) cv2.waitKey(0) cv2.destroyAllWindows()#close window ============================ data used in this video #===================================# if...
Image
   Computer vision: calculte the vehicle  speed using open CV | Computer vision  arab import cv2 import time import os  os.chdir(r"C:\Users\amb\Downloads") cascade_src = r'C:\Users\amb\Downloads\cars1.xml' video_src = r'C:\Users\amb\Downloads\video3.MP4' #line a ax1=70 ay=90 ax2=230 #line b bx1=15 by=125 bx2=225 def Speed_Cal(time):       try:                 Speed = (9.144/1000)/(time/3600)                         return Speed                      except ZeroDivisionError :                 print ("can not devide by zero")                           #car num i = 1 start_time = time.time() #video .... cap = cv2.VideoCapture(video_src) car_cascade = cv2.CascadeClassifier(casc...
Image
  Computer vision: Canny Edge Detection in OpenCV Arabic Code 1 ========== import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread("lena.jpg") img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) canny = cv2.Canny(img, 100, 200) titles = ['image', 'canny'] images = [img, canny] for i in range(2):     plt.subplot(1, 2, i+1), plt.imshow(images[i], 'gray')          plt.title(titles[i])          plt.xticks([]),plt.yticks([])#ticks      plt.show()#show =================================================== Code 2  =================================================== import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread("messi5.jpg", cv2.IMREAD_GRAYSCALE) lap = cv2.Laplacian(img, cv2.CV_64F, ksize=3) lap = np.uint8(np.absolute(lap)) sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0) sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1) edges = cv2.Canny(img,100,200) sobelX = np.uint8(np...
Image
      Computer vision: Sobel X and Sobel y edge detection Arabic   import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread("sudoku.png", cv2.IMREAD_GRAYSCALE) lap = cv2.Laplacian(img, cv2.CV_64F, ksize=3) lap = np.uint8(np.absolute(lap)) sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0) sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1) sobelX = np.uint8(np.absolute(sobelX)) sobelY = np.uint8(np.absolute(sobelY)) sobelCombined = cv2.bitwise_or(sobelX, sobelY) titles = ['image', 'Laplacian', 'sobelX', 'sobelY', 'sobelCombined'] images = [img, lap, sobelX, sobelY, sobelCombined] for i in range(5):     plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray')           plt.title(titles[i])           plt.xticks([]),plt.yticks([])#ticks       plt.show() #show #===================================# data used in this video #===================================# if you faced any is...
Image
  Computer vision: Image Gradients and Edge Detection using Laplacian  import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread("sudoku.png", cv2.IMREAD_GRAYSCALE) lap = cv2.Laplacian(img, cv2.CV_64F, ksize=3) lap = np.uint8(np.absolute(lap)) sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0) sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1) sobelX = np.uint8(np.absolute(sobelX)) sobelY = np.uint8(np.absolute(sobelY)) sobelCombined = cv2.bitwise_or(sobelX, sobelY) titles = ['image', 'Laplacian', 'sobelX', 'sobelY', 'sobelCombined'] images = [img, lap, sobelX, sobelY, sobelCombined] for i in range(5):     plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray')           plt.title(titles[i])           plt.xticks([]),plt.yticks([])#ticks       plt.show() #show #===================================# data used in this video #===================================# if you faced any issue co...
Image
  Computer vision: Smoothing Images | Blurring Images OpenCV  import numpy as np from matplotlib import pyplot as plt import cv2 img = cv2 . imread ( 'lena.jpg' ) img = cv2 . cvtColor ( img , cv2 . COLOR_BGR2RGB ) kernel = np . ones (( 5 , 5 ), np . float32 ) / 25 dst = cv2 . filter2D ( img , - 1 , kernel ) blur = cv2 . blur ( img , ( 5 , 5 )) gblur = cv2 . GaussianBlur ( img , ( 5 , 5 ), 0 ) median = cv2 . medianBlur ( img , 5 ) bilateralFilter = cv2 . bilateralFilter ( img , 9 , 75 , 75 ) titles = [ 'image' , '2D Convolution' , 'blur' , 'GaussianBlur' , 'median' , 'bilateralFilter' ] images = [ img , dst , blur , gblur , median , bilateralFilter ] for i in range ( 6 ): plt . subplot ( 2 , 3 , i + 1 ), plt . imshow ( images [ i ], 'gray' ) plt . title ( titles [ i ]) plt . xticks ([]), plt . yticks ([])#ticks plt . show ()#show #===================================# data u...