├── .idea ├── misc.xml ├── modules.xml ├── object _distance.iml └── vcs.xml ├── README.md ├── methods.py └── object_distance.py /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/object _distance.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 13 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenCv_Object_distance 2 | Here, we use opencv to find out the distance between the camera and the object by using a single camera/webcam. 3 | By default it will capture the camera. You can change it by giving arg while tuning or changing the parm in the following lines. 4 | 5 | ```python 6 | parser.add_argument('--webcam', type=bool, default=True) 7 | parser.add_argument('--url', type=str, default='http://192.168.0.4:8080/shot.jpg') 8 | ''' 9 | -------------------------------------------------------------------------------- /methods.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def find_marker(image): 5 | # convert the image to grayscale and blur to detect edges 6 | 7 | blurred_frame = cv2.GaussianBlur(image, (5, 5), 0) 8 | hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV) 9 | 10 | lower_blue = np.array([38, 86, 0]) 11 | upper_blue = np.array([121, 255, 255]) 12 | mask = cv2.inRange(hsv, lower_blue, upper_blue) 13 | 14 | contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 15 | con = max(contours, key=cv2.contourArea) 16 | 17 | return cv2.minAreaRect(con) 18 | 19 | def distance_to_camera(knownWidth, focalLength, perWidth): 20 | # compute and return the distance from the image to the camera 21 | return (knownWidth * focalLength) / perWidth 22 | -------------------------------------------------------------------------------- /object_distance.py: -------------------------------------------------------------------------------- 1 | import time 2 | import urllib.request 3 | from methods import * 4 | import argparse 5 | 6 | parser = argparse.ArgumentParser() 7 | parser.add_argument('--webcam', type=bool, default=True) 8 | parser.add_argument('--url', type=str, default='http://192.168.0.4:8080/shot.jpg') 9 | 10 | args = parser.parse_args() 11 | #here we put the object 30 cm from the camera to calibraing 12 | KNOWN_DISTANCE = 30.0 13 | 14 | #the width of the object in cm 15 | KNOWN_WIDTH = 3.0 16 | #we use a ip webcam for capture video 17 | 18 | url = args.url 19 | print("<<<---- taking calibrating Image ---->>>") 20 | time.sleep(2) 21 | if args.webcam: 22 | cap = cv2.VideoCapture(0) 23 | _,c_image = cap.read() 24 | 25 | 26 | else: 27 | imgResp = urllib.request.urlopen(url) 28 | imgNp = np.array(bytearray(imgResp.read()), dtype=np.uint8) 29 | c_image = cv2.imdecode(imgNp, -1) 30 | 31 | 32 | 33 | marker = find_marker(c_image) 34 | focalLength = (marker[1][0] * KNOWN_DISTANCE) / KNOWN_WIDTH 35 | 36 | 37 | time.sleep(2) 38 | 39 | print("<<<---- Main program Staring ---->>>") 40 | 41 | while True: 42 | if args.webcam: 43 | _,image = cap.read() 44 | else: 45 | imgResp = urllib.request.urlopen(url) 46 | image = np.array(bytearray(imgResp.read()), dtype=np.uint8) 47 | # image = cv2.imdecode(imgNp, -1) 48 | 49 | 50 | 51 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 52 | marker = find_marker(image) 53 | CM = distance_to_camera(KNOWN_WIDTH, focalLength, marker[1][0]) 54 | 55 | #print the output 56 | cv2.putText(image, "%.2fcm" % CM, 57 | (image.shape[1] - 350, image.shape[0] - 15), cv2.FONT_HERSHEY_SIMPLEX, 58 | 2.0, (255, 0, 0), 3) 59 | cv2.imshow("image", image) 60 | key = cv2.waitKey(1) 61 | #Press Esc to stop the program 62 | if key == 27: 63 | break 64 | 65 | 66 | cv2.destroyAllWindows() 67 | --------------------------------------------------------------------------------