Giter Club home page Giter Club logo

Comments (6)

yu4u avatar yu4u commented on July 28, 2024

Hi @Jeanbverr,

The pretrained_model was trained with the IMDB dataset.
Does it work for webcam images on your environment?
The image size might not be the problem because the image size is also quite different from that in training in demo.py and somehow it works (at least on my environment).
Is face detection also performed on your modified demo.py code?

from age-gender-estimation.

jeanbverroken avatar jeanbverroken commented on July 28, 2024

Hello, thank you for your response!

Yes, it works for webcam images on my environment, but there is a high fluctuation in age estimation (ranging from 20-30-ish)

Yes I have a face detection. My code looks as follows:

`import os
import cv2
import dlib
import numpy as np
import argparse
from contextlib import contextmanager
from wide_resnet import WideResNet
from keras.utils.data_utils import get_file

pretrained_model = "https://github.com/yu4u/age-gender-estimation/releases/download/v0.5/weights.18-4.06.hdf5"
modhash = '89f56a39a78454e96379348bddd78c0d'


def get_args():
	parser = argparse.ArgumentParser(description="This script detects faces from web cam input, "
												 "and estimates age and gender for the detected faces.",
									 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
	parser.add_argument("--weight_file", type=str, default=None,
						help="path to weight file (e.g. weights.18-4.06.hdf5)")
	parser.add_argument("--depth", type=int, default=16,
						help="depth of network")
	parser.add_argument("--width", type=int, default=8,
						help="width of network")
	parser.add_argument(
        '-f',
        '--file',
        help='Path for input file. First line should contain number of lines to search in'
    )
	args = parser.parse_args("AAA --file /path/to/sequences.txt".split())

	return args


def draw_label(image, point, label, font=cv2.FONT_HERSHEY_SIMPLEX,
               font_scale=1, thickness=2):
	size = cv2.getTextSize(label, font, font_scale, thickness)[0]
	x, y = point
	cv2.rectangle(image, (x, y - size[1]), (x + size[0], y), (255, 0, 0), cv2.FILLED)
	cv2.putText(image, label, point, font, font_scale, (255, 255, 255), thickness)


def main(depth = 16, k = 8, weight_file = None):
	if not weight_file:
		weight_file = get_file("weights.18-4.06.hdf5", pretrained_model, cache_subdir="pretrained_models",
							   file_hash=modhash, cache_dir=os.path.dirname(os.path.abspath(__file__)))
	
	# for face detection
	detector = dlib.get_frontal_face_detector()

	# load model and weights
	img_size = 64
	model = WideResNet(img_size, depth=depth, k=k)()
	model.load_weights(weight_file)

	img = os.path.abspath("/path/img")
	input_img = cv2.imread(img, 1)
	# cv2.imshow("img",input_img)
	img_h, img_w, _ = np.shape(input_img)
	detected = detector(input_img, 1)
	faces = np.empty((len(detected), img_size, img_size, 3))
	
	if len(detected) > 0:
		for i, d in enumerate(detected):
			x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
			xw1 = max(int(x1 - 0.4 * w), 0)
			yw1 = max(int(y1 - 0.4 * h), 0)
			xw2 = min(int(x2 + 0.4 * w), img_w - 1)
			yw2 = min(int(y2 + 0.4 * h), img_h - 1)
			cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
			# cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
			faces[i, :, :, :] = cv2.resize(input_img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))

		# predict ages and genders of the detected faces
		results = model.predict(faces)
		predicted_genders = results[0]
		ages = np.arange(0, 101).reshape(101, 1)
		predicted_ages = results[1].dot(ages).flatten()

		# draw results
		for i, d in enumerate(detected):
			label = "{}, {}".format(int(predicted_ages[i]),
									"F" if predicted_genders[i][0] > 0.5 else "M")
			draw_label(input_img, (d.left(), d.top()), label)

	# input_img = cv2.resize(input_img, dsize=tuple([s // 2 for s in image.shape if s > 3])[::-1])
	if(img_h>2000):
		new_h = int(img_h/3)
		new_w = int(img_w/3)
		input_img = cv2.resize(input_img, (new_w, new_h))
	cv2.imshow("result", input_img)
	key = cv2.waitKey()
if __name__ == '__main__':
	print("test")
	main()
`

from age-gender-estimation.

yu4u avatar yu4u commented on July 28, 2024

I see.
Are you using TensorFlow backend?
If so, the problem might be simply the accuracy of the model, which I should work on to improve...

from age-gender-estimation.

jeanbverroken avatar jeanbverroken commented on July 28, 2024

Yes, TensorFlow as backend.
It surprises me because it's been trained on half a million images.
Thanks so much for the response though.
I saw your project to be the most reliable one, do you know an alternative, perhaps?

from age-gender-estimation.

yu4u avatar yu4u commented on July 28, 2024

For gender estimation, the following project is very cool.

https://github.com/oarriaga/face_classification

I don't know the project for age estimation with reproducible codes...
If you find good one, please let me know :P

from age-gender-estimation.

jeanbverroken avatar jeanbverroken commented on July 28, 2024

Alright. Thanks for the response, Ill look into it. It looks promising!

from age-gender-estimation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.