Created
June 22, 2020 19:03
-
-
Save GeneralistDev/34c88731586f48632ebc33002b7433c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
from flask import Flask,jsonify, request | |
from flask_cors import CORS | |
import os | |
import sys | |
import cv2 | |
from collections import Counter | |
from datetime import datetime | |
import face_recognition | |
app = Flask(__name__) | |
cors = CORS(app, resources={r"*": {"origins": "*"}}) | |
class BackgroundColorDetector(): | |
def __init__(self, imageLoc): | |
self.img = cv2.imread(imageLoc, 1) | |
self.manual_count = {} | |
self.w, self.h, self.channels = self.img.shape | |
self.total_pixels = self.w*self.h | |
print(self.total_pixels) | |
def count(self): | |
for y in range(0, self.h): | |
for x in range(0, self.w): | |
RGB = (self.img[x//2, y//2, 2], self.img[x//2, y//2, 1], self.img[x//2, y//2, 0]) | |
if RGB in self.manual_count: | |
self.manual_count[RGB] += 1 | |
else: | |
self.manual_count[RGB] = 1 | |
def average_colour(self): | |
red = 0 | |
green = 0 | |
blue = 0 | |
sample = 10 | |
for top in range(0, sample): | |
red += self.number_counter[top][0][0] | |
green += self.number_counter[top][0][1] | |
blue += self.number_counter[top][0][2] | |
average_red = red / sample | |
average_green = green / sample | |
average_blue = blue / sample | |
print("Average RGB for top ten is: (", average_red, | |
", ", average_green, ", ", average_blue, ")") | |
return (average_red, average_green, average_blue) | |
def twenty_most_common(self): | |
self.count() | |
self.number_counter = Counter(self.manual_count).most_common(20) | |
for rgb, value in self.number_counter: | |
print(rgb, value, ((float(value)/self.total_pixels)*100)) | |
def detect(self): | |
self.twenty_most_common() | |
self.percentage_of_first = ( | |
float(self.number_counter[0][1])/self.total_pixels) | |
print(self.percentage_of_first) | |
if self.percentage_of_first > 0.5: | |
print("Background color is ", self.number_counter[0][0]) | |
return self.number_counter[0][0] | |
else: | |
return self.average_colour() | |
@app.route("/file_upload", methods=["POST"]) | |
def upload_file(): | |
try: | |
binary_data = [] | |
photo_1 = request.files.get("photo_cap_1") | |
photo_base64_1 = base64.b64encode(photo_1.read()) | |
encoded1 = photo_base64_1 | |
binary_data.append(base64.b64decode(encoded1)) | |
if not os.path.isdir("images"): | |
os.makedirs("images") | |
image_name = datetime.now().strftime("%Y-%m-%d %H-%M-%S") + ".jpeg" | |
with open(os.path.join("images", image_name), "wb") as f: | |
f.write(binary_data[0]) | |
print(image_name) | |
image = face_recognition.load_image_file(os.path.join(os.getcwd(), "images", image_name)) | |
face_bounding_boxes = face_recognition.face_locations(image) | |
if len(face_bounding_boxes) == 0: | |
response = { | |
"image_validity": 0, | |
"status": "201", | |
"msg": 'Face not found !' | |
} | |
return jsonify(response) | |
elif len(face_bounding_boxes) == 1: | |
BackgroundColor = BackgroundColorDetector(os.path.join(os.getcwd(), "images", image_name)) | |
average_color = BackgroundColor.detect() | |
print(average_color) | |
r_min = 165 #175 | |
r_max = 255 | |
g_min = 180 | |
g_max = 255 | |
b_min = 173 | |
b_max = 255 | |
if r_min <= average_color[0] <= r_max and g_min <= average_color[1] <= g_max and b_min <= average_color[2] <= b_max: | |
msg = "This image has a absolute white background color" | |
else: | |
msg = "This image does not match with most instances of white background color" | |
response = { | |
"image_validity": 1, | |
"status": "200", | |
"msg": msg, | |
"rgb": average_color | |
} | |
return jsonify(response) | |
except Exception as e: | |
exc_type, exc_obj, exc_tb = sys.exc_info() | |
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] | |
print(exc_type, fname, exc_tb.tb_lineno) | |
print(str(e)) | |
response = { | |
"image_validity": 0, | |
"status": "400", | |
"msg": "Something went wrong." | |
} | |
return jsonify(response) | |
if __name__=='__main__': | |
app.run(host='0.0.0.0', port=5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment