File size: 3,779 Bytes
26f8ecf
 
 
 
 
 
59907f0
 
bc5a08a
 
 
 
 
 
 
59907f0
bc5a08a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c85583
bc5a08a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import gradio as gr

def greet(name):
    return "Hello " + name + "!!"

iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()

import datetime
import time
from skimage import io, color
from PIL import Image
from io import BytesIO
from skimage.io import imread, imsave
import imutils

# Location of files on your system
PATH_TO_FILES = '/home/ekene/Documents/working/ChoiceData/imglb2017/'

# Start by importing modules
from glob import glob
import cv2
import datetime
import sys
import os
import string
import warnings
from os.path import join
import matplotlib.pyplot as plt

def main(argv):
    timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S%f")
    print("Initialising...")
    if len(argv) == 2:
        filename = argv[1]
        full_path = PATH_TO_FILES + filename
        img = imread(full_path)

        scale_image = 2 # downscale the image
        img = cv2.resize(img, (scale_image, scale_image))

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        #otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        #img = cv2.cvtColor(otsu, cv2.COLOR_GRAY2BGR)

        # Apply histogram equalization
        h, w = gray.shape[:2]
        cv_he = cv2.equalizeHist(gray)
        #cv2.imshow('HE', cv_he)


        _, contours, hierarchy = cv2.findContours(cv_he, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

       

        # sort contours by contour area
         # keep only contours that are larger than 10% of the image area
        # (here we assume the largest contour will be around the "head")

        filtered_contours = [c for c in contours if cv2.contourArea(c) > 1000]

         #calculate distance between each contour and sort contours by distance
        # print(filtered_contours, contours)
        closest_contours = sorted(filtered_contours, key = cv2.contourArea, reverse=True)
        closest_contours = closest_contours[:4]
        # selected_contours = closest_contours

        # sort by smallest contour area
        #filtered_contours = sorted(filtered_contours, key = cv2.contourArea, reverse=True)

        # used for testing
        if len(filtered_contours) > 1:
           return 0
        cv2.drawContours(gray, [filtered_contours[0]], -1, (255,255,255), 2)
        cv2.drawContours(gray, filtered_contours, -1, (255,255,255), 2)
        cv2.imshow("search by smallest area", gray)
        cv2.waitKey(0)

        return 0

# adding to show a function image

def show_image(frame):
    plt.subplot(1,2,1)
    plt.title('Original Image')
    plt.imshow(frame, cmap='gray')
      plt.subplot(1,2,2)
    plt.title('Image with contours drawn')
    plt.imshow(frame, cmap='gray')
    plt.show()
    return 0

def image_face_detection(path_to_image):

    # Initialise the trackbar
    cv2.namedWindow('Frame')
    cv2.namedWindow('Threshold')

    current_image = cv2.imread(path_to_image)

    frame = current_image
    # threshold_trackbar = cv2.createTrackbar('Threshold', 'Frame', 51, 255, nothing)
    # cv2.createTrackbar('Threshold', 'Frame', 100, 255, nothing)
    # cv2.createTrackbar('Threshold', 'Frame', 130, 255, nothing)
    # cv2.createTrackbar('Threshold', 'Frame', 150, 255, nothing)
    # cv2.createTrackbar('Threshold', 'Frame', 90, 255, nothing)
    # cv2.createTrackbar('Threshold', 'Frame', 180, 255, nothing)

    #cv2.namedWindow("Frame")
     #cv2.createTrackbar('Threshold', "Frame", 50, 255, nothing)

    # Initialize the first frame, resize the window, start the timer
    # first_time = True
    first_time = True
    # start_clock = time.time()
    # main_loop()
    # print('Run time ', time.time()-start_clock)
    start_clock = time.time()

    # Capture keyboard events
    key = cv2.waitKey(1) & 0xFF
    # Press space to pause or unpause the video