diff --git a/opencv.ipynb b/opencv.ipynb new file mode 100644 index 0000000..4173cb9 --- /dev/null +++ b/opencv.ipynb @@ -0,0 +1,87 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import cv2\n", + "\n", + "#load some pretrained data on face frontals opencv supplied\n", + "trained_face_data= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')\n", + "\n", + "#to read an image to detect faces\n", + "#img=cv2.imread('MSD.jpg')\n", + "webcam=cv2.VideoCapture(0)\n", + "\n", + "\n", + "\n", + "while True:\n", + " \n", + " success_frame_read, frame=webcam.read()\n", + "#to show img\n", + "#cv2.imshow('face detec',img)\n", + "\n", + "#got to make image black as we are using harcascade algo , it uses gray scale \n", + "\n", + " grayscaled_img=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\n", + "\n", + " cv2.imshow('images',grayscaled_img)\n", + "\n", + "#now we need to plug our image to the algo, so we use trained data\n", + "#detect faces\n", + "#face_coordinates=trained_face_data.detectMultiScale(grayscaled_img)\n", + "\n", + "#print(face_coordinates)\n", + "#detectmultiscale detects objects of diff sizes in the input img. and returned as rectangles\n", + "\n", + "\n", + "#draw rectangles around the faces (x,y,w,h) coordinates\n", + "#cv2.rectangle(img,(x,y),(x+w,y+h),(r,g,b), rect thickness)\n", + "\n", + "#cv2.rectangle(img,(660,62),(660+349,62+349),(0,255,0),3)\n", + "\n", + "\n", + "#cv2.imshow('show',img) to show img with rec\n", + "#waits for image to open\n", + " key=cv2.waitKey(2)\n", + " \n", + " if key==81 or key==113:\n", + " break\n", + " \n", + "webcam.release()\n", + "\n", + "#if you have images next to next use for loop \n", + "#for (x,y,w,h) in face_coordinates:\n", + " # cv2.rectangle(img,(x,y),(x+w,y+h),(r,g,b), rect thickness)\n", + "\n", + "#to capture video from webcam\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "q\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}