Commit 1d11de18 authored by P.M.P.C Bandara's avatar P.M.P.C Bandara

Updated measure distance file

parent 70f29e73
# install opencv "pip install opencv-python"
import cv2
# distance from camera to object(frame) measured
# centimeter
Known_distance = 76.2
# width of frame in the real world or Object Plane
# centimeter
Known_width = 14.3
# Colors
GREEN = (0, 255, 0)
RED = (0, 0, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# defining the fonts
fonts = cv2.FONT_HERSHEY_COMPLEX
# frame detector object
frame_detector = cv2.CascadeClassifier("haarcascade_frontalframe_default.xml")
# focal length finder function
def Focal_Length_Finder(measured_distance, real_width, width_in_rf_image):
# finding the focal length
focal_length = (width_in_rf_image * measured_distance) / real_width
return focal_length
# distance estimation function
def Distance_finder(Focal_Length, real_frame_width, frame_width_in_frame):
distance = (real_frame_width * Focal_Length) / frame_width_in_frame
# return the distance
return distance
def frame_data(image):
frame_width = 0 # making frame width to zero
# converting color image to gray scale image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detecting frame in the image
frames = frame_detector.detectMultiScale(gray_image, 1.3, 5)
# looping through the frames detect in the image
# getting coordinates x, y , width and height
for (x, y, h, w) in frames:
# draw the rectangle on the frame
cv2.rectangle(image, (x, y), (x + w, y + h), GREEN, 2)
# getting frame width in the pixels
frame_width = w
# return the frame width in pixel
return frame_width
# reading reference_image from directory
ref_image = cv2.imread("Ref_image.png")
# find the frame width(pixels) in the reference_image
ref_image_frame_width = frame_data(ref_image)
# get the focal by calling "Focal_Length_Finder"
# frame width in reference(pixels),
# Known_distance(centimeters),
# known_width(centimeters)
Focal_length_found = Focal_Length_Finder(
Known_distance, Known_width, ref_image_frame_width)
print(Focal_length_found)
# show the reference image
cv2.imshow("ref_image", ref_image)
# initialize the camera object so that we
# can get frame from it
cap = cv2.VideoCapture(0)
# looping through frame, incoming from
# camera/video
while True:
# reading the frame from camera
_, frame = cap.read()
# calling frame function to find
# the width of frame(pixels) in the frame
frame_width_in_frame = frame_data(frame)
# check if the frame is zero then not
# find the distance
if frame_width_in_frame != 0:
# finding the distance by calling function
# Distance finder function need
# these arguments the Focal_Length,
# Known_width(centimeters),
# and Known_distance(centimeters)
Distance = Distance_finder(
Focal_length_found, Known_width, frame_width_in_frame)
# draw line as background of text
cv2.line(frame, (30, 30), (230, 30), RED, 32)
cv2.line(frame, (30, 30), (230, 30), BLACK, 28)
# Drawing Text on the screen
cv2.putText(
frame, f"Distance: {round(Distance, 2)} CM", (30, 35),
fonts, 0.6, GREEN, 2)
# show the frame on the screen
cv2.imshow("frame", frame)
# quit the program if you press 'q' on keyboard
if cv2.waitKey(1) == ord("q"):
break
# closing the camera
cap.release()
# closing the windows that are opened
cv2.destroyAllWindows()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment