{"id":16430421,"url":"https://github.com/nikhil22/python-vehicle-detection","last_synced_at":"2025-08-31T22:42:20.403Z","repository":{"id":95671761,"uuid":"93976354","full_name":"Nikhil22/python-vehicle-detection","owner":"Nikhil22","description":"Vehicle detection in Python","archived":false,"fork":false,"pushed_at":"2017-06-11T02:20:41.000Z","size":60930,"stargazers_count":9,"open_issues_count":1,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-08T03:51:07.483Z","etag":null,"topics":["nanodegree","pandas","python","svm-classifier","udacity","vehicle-detection"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Nikhil22.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-11T02:12:47.000Z","updated_at":"2022-11-12T08:16:14.000Z","dependencies_parsed_at":"2023-04-03T11:33:25.076Z","dependency_job_id":null,"html_url":"https://github.com/Nikhil22/python-vehicle-detection","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Nikhil22/python-vehicle-detection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikhil22%2Fpython-vehicle-detection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikhil22%2Fpython-vehicle-detection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikhil22%2Fpython-vehicle-detection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikhil22%2Fpython-vehicle-detection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nikhil22","download_url":"https://codeload.github.com/Nikhil22/python-vehicle-detection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikhil22%2Fpython-vehicle-detection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273051868,"owners_count":25037078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-31T02:00:09.071Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nanodegree","pandas","python","svm-classifier","udacity","vehicle-detection"],"created_at":"2024-10-11T08:26:55.445Z","updated_at":"2025-08-31T22:42:20.392Z","avatar_url":"https://github.com/Nikhil22.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **Vehicle Detection**\n\n[//]: # (Image References)\n[image1]: ./examples/car_not_car.png\n[image2]: ./examples/HOG_example.jpg\n[image3]: ./examples/sliding_windows.jpg\n[image4]: ./examples/sliding_window.jpg\n[image5]: ./examples/bboxes_and_heat.png\n[image6]: ./examples/labels_map.png\n[image7]: ./examples/output_bboxes.png\n[video1]: ./project_video.mp4\n[//]: # (Image References)\n\n[one]: ./output_images/one.png \n[two]: ./output_images/two.png \n[three]: ./output_images/three.png \n\n## Final output\n\n![demo](result.gif)\n\n\n## Histogram of Oriented Gradients (HOG)\n\nExample of vehicle and non vehicle\"\n\n![alt text][image1]\n\nI created a function called get_hog_features. After a bit of research, I found that I could use cv2.HOGDescriptor, and provide a feature space to it. For getting the feature space of an image, here's a code snippet\n\n```python\ndef get_feature_space(img, cspace):\n    if cspace != 'RGB':\n        if cspace == 'HLS':\n            features = cv2.cvtColor(img, cv2.COLOR_RGB2HLS)\n        elif cspace == 'YCrCb':\n            features = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)\n        elif cspace == 'HSV':\n            features = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)\n        elif cspace == 'LUV':\n            features = cv2.cvtColor(img, cv2.COLOR_RGB2LUV)\n        elif cspace == 'YUV':\n            features = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)\n        elif cspace == 'Lab':\n            features = cv2.cvtColor(img, cv2.COLOR_RGB2Lab)\n        return features\n\ndef get_hog_features(img, cspace):\n    return np.ravel(\n        cv2.HOGDescriptor((64,64), (16,16), (8,8), (8,8), 9) \\\n            .compute(get_feature_space(img, cspace))\n    )\n```\n\n![alt text][two]\n\n### 2. Final choice of HOG parameters.\n\nFirst, I defined a function extract_features get_hog_features. This function loops through all images, and creates an array of hogs features of each image. This array is then used as the feature array for training.  Here's a code snippet:\n\n```python\ndef extract_features(imgs, cspace='RGB', size = (64,64)):\n    features = []\n    for filename in imgs:\n        image = imread(filename)\n        if size != (64,64):\n            image = cv2.resize(image, size)\n        features.append(\n            np.ravel(\n                cv2.HOGDescriptor((64,64), (16,16), (8,8), (8,8), 9) \\\n                    .compute(get_feature_space(image, cspace))\n            )\n        )\n    return features\n```\n\n\nOf all color spaces, YUV was the best at detecting vehicles. \nI normalized and split by data into train and test sets.\n\n### 3. Training a classifier using selected HOG features.\n\nI trained using both an SVM and an MLP. MLP had a higher test accuracy. Here are the results. \n\n|Classifier|Training Accuracy|Test Accuracy|\n|----------|-----------------|-------------|\n|svm |1.00|.950|\n|mlp |1.00|.9926|\n\n###Sliding Window Search\n\n### 1. Sliding window search, scales, and overlaps. \n\nI did a bit of research to look for and modify an efficient and accurate sliding window algorithm.\n\n1. get HOGS features for each window\n2. only search for vehicle in the bottom half of image\n3. multiple window scaled, to ensure we detect both closeby and distant images. \n4. 80% xy overlap, through trial and error\n\n```python\ndef slide_window(img, x_start_stop=[None, None], y_start_stop=[None, None], \n                    xy_window=(64, 64), xy_overlap=(0.75, 0.75)):\n    if x_start_stop[0] == None:\n        x_start_stop[0] = 0\n    if x_start_stop[1] == None:\n        x_start_stop[1] = img.shape[1]\n    if y_start_stop[0] == None:\n        y_start_stop[0] = 0\n    if y_start_stop[1] == None:\n        y_start_stop[1] = img.shape[0]\n    xspan = x_start_stop[1] - x_start_stop[0]\n    yspan = y_start_stop[1] - y_start_stop[0]\n    nx_pix_per_step = np.int(xy_window[0]*(1 - xy_overlap[0]))\n    ny_pix_per_step = np.int(xy_window[1]*(1 - xy_overlap[1]))\n    nx_windows = np.int(xspan/nx_pix_per_step) \n    ny_windows = np.int(yspan/ny_pix_per_step)\n    window_list = []\n    for ys in range(ny_windows):\n        for xs in range(nx_windows):\n            startx = xs*nx_pix_per_step + x_start_stop[0]\n            endx = (xs+1)*nx_pix_per_step + x_start_stop[0]\n            starty = ys*ny_pix_per_step + y_start_stop[0]\n            endy = (ys+1)*ny_pix_per_step + y_start_stop[0]\n            window_list.append(((startx, starty), (endx, endy)))\n    return window_list\n```\n\n![alt text][one]\n![alt text][three]\n---\n\n### Video Implementation\n\nHere's a [link to my video result](./result.mp4)\n\nThe MLP has a method called predict_proba which returns the confidence/probability of each class.\nOnly classifications with a score \u003e0.99 where chosen.\n\n\n### Aftermath\n\nThis pipleline may fail when trying to deteced motorcycles or bicycles. To fix this, we would have to append our trainining and test sets with images of classified images of bikes, etc and adjust our feature extraction algorithm.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikhil22%2Fpython-vehicle-detection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikhil22%2Fpython-vehicle-detection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikhil22%2Fpython-vehicle-detection/lists"}