I tried object detection using Python and OpenCV

Introduction

I detected and extracted the face here, but I thought that I could realize it by copying without thinking too much. This time, I wanted to try to detect objects other than the face, so I tried to imitate it even though I didn't have much knowledge.

I made a face classifier for Dir en gray using TensorFlow-④ Face extraction

reference

opencv tutorial challenge 9 outline (area) in OpenCV

environment

Have a look at this.

I made a face classifier for Dir en gray using TensorFlow --② Environment construction

Image to use

This is Apo.

1401205top.jpg

result

The following three are detected.

スクリーンショット 2017-06-01 9.48.42.png

The following three rectangular images are output.

1.jpg

2.jpg

0.jpg

Source code

contour_detect.py


#!/usr/local/bin/python
#! -*- coding: utf-8 -*-

import cv2
import numpy as np

#Specified image(path)Detects the object of and outputs the image of the circumscribing rectangle
def detect_contour(path):

  #Load image
  src = cv2.imread(path, cv2.IMREAD_COLOR)

  #Convert to grayscale image
  gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

  #Binarization
  retval, bw = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

  #Extract contour
  #   contours : [region][Point No][0][x=0, y=1]
  #   cv2.CHAIN_APPROX_NONE:Hold the midpoint
  #   cv2.CHAIN_APPROX_SIMPLE:Do not hold midpoint
  contours, hierarchy = cv2.findContours(bw, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)

  #Rectangle detected number (default is 0)
  detect_count = 0

  #Processing for each contour
  for i in range(0, len(contours)):

    #Calculate contour area
    area = cv2.contourArea(contours[i])

    #Exclude noise (too small area) and overall contour (too large area)
    if area < 1e2 or 1e5 < area:
      continue

    #External rectangle
    if len(contours[i]) > 0:
      rect = contours[i]
      x, y, w, h = cv2.boundingRect(rect)
      cv2.rectangle(src, (x, y), (x + w, y + h), (0, 255, 0), 2)

      #Save image for each circumscribing rectangle
      cv2.imwrite('{File Path}' + str(detect_count) + '.jpg', src[y:y + h, x:x + w])

      detect_count = detect_count + 1

  #Display circumscribed rectangular image
  cv2.imshow('output', src)
  cv2.waitKey(0)

  #End processing
  cv2.destroyAllWindows()

if __name__ == '__main__':
  detect_contour('{File Path}/{file name}.jpg')

Other things I tried and didn't work

スクリーンショット 2017-06-01 9.45.40.png スクリーンショット 2017-06-01 9.50.15.png

Summary

--Read the rectangle as "Kukei" instead of "Tankei". --If there are too many objects, the limit of extraction will come out (?). ――Isn't it possible to erase the overlapping contour lines when cutting?

Next step

--Investigate how to erase the outline. ――Check the calculation formula to improve the detection accuracy a little more. ――Let's learn an object by applying TensorFlow.

All page links

-I tried object detection using Python and OpenCV -I tried to sort out objects from the image of steak set meal-① Object detection -I tried to sort out the objects from the image of the steak set meal-② Overlap number sorting -I tried to sort out the objects from the image of the steak set meal-③ Similar image heat map detection -I tried to sort out the objects from the image of the steak set meal-④ Clustering -I tried to sort out objects from the image of steak set meal-⑤ Similar image feature point detection edition

Recommended Posts

I tried object detection using Python and OpenCV
I tried web scraping using python and selenium
[Python] I tried using OpenPose
[Python] Using OpenCV with Python (Edge Detection)
I tried using GrabCut of OpenCV
I tried using Thonny (Python / IDE)
[Python] I tried using YOLO v3
[For beginners] I tried using the Tensorflow Object Detection API
I tried using PyEZ and JSNAPy. Part 2: I tried using PyEZ
I tried using Bayesian Optimization in Python
I tried non-photorealistic rendering with Python + opencv
I tried using UnityCloudBuild API from Python
Pydroid 3 --I tried OpenCV and TensorFlow options for IDE for Python 3 (Android)
[Image processing] Edge detection using Python and OpenCV makes Poo naked!
I just erased the object using image repair (inpaint) (OpenCV: Python)
I tried using parameterized
Hello World and face detection with OpenCV 4.3 + Python
I tried using PyEZ and JSNAPy. Part 1: Overview
I tried "smoothing" the image with Python + OpenCV
I tried using argparse
I tried using mimesis
I tried using anytree
vprof --I tried using the profiler for Python
I tried "differentiating" the image with Python + OpenCV
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
[ML-Aents] I tried machine learning using Unity and Python TensorFlow (v0.11β compatible)
I tried using matplotlib
I tried using "Anvil".
I tried Jacobian and partial differential with python
I tried using Hubot
I tried using mecab with python2.7, ruby2.3, php7
I tried function synthesis and curry with python
I tried using ESPCN
I tried using openpyxl
I tried "binarizing" the image with Python + OpenCV
I tried using Ipython
I tried using PyCaret
I tried reading a CSV file using Python
I tried using cron
I tried using the Datetime module by Python
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried Python> decorator
I tried using the image filter of OpenCV
I tried to create a sample to access Salesforce using Python and Bottle
I tried using folium
I tried using jinja2
I tried using folium
I tried updating Google Calendar with CSV appointments using Python and Google APIs
I tried using time-window
Python programming: I tried to get (crawling) news articles using Selenium and BeautifulSoup4.
I tried using Google Translate from Python and it was just too easy
I tried running an object detection tutorial using the latest deep learning algorithm
I tried pipenv and asdf for Python version control