Yam Code
Sign up
Login
New paste
Home
Trending
Archive
English
English
Tiếng Việt
भारत
Sign up
Login
New Paste
Browse
# author: Arun Ponnusamy # website: https://www.arunponnusamy.com # import necessary packages from keras.preprocessing.image import img_to_array from keras.models import load_model from keras.utils import get_file import numpy as np import random import argparse import cv2 import os import cvlib as cv # download pre-trained model file (one-time download) dwnld_link = "https://github.com/arunponnusamy/cvlib/releases/download/v0.2.0/gender_detection.model" model_path = get_file("gender_detection.model", dwnld_link, cache_subdir="pre-trained", cache_dir=os.getcwd()) # load model model = load_model(model_path) # open webcam webcam = cv2.VideoCapture(0) if not webcam.isOpened(): print("Could not open webcam") exit() classes = ['man', 'woman'] # loop through frames while webcam.isOpened(): # read frame from webcam status, frame = webcam.read() if not status: print("Could not read frame") exit() # apply face detection face, confidence = cv.detect_face(frame) print(face) print(confidence) # loop through detected faces for idx, f in enumerate(face): # get corner points of face rectangle (startX, startY) = f[0], f[1] (endX, endY) = f[2], f[3] # draw rectangle over face cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2) # crop the detected face region face_crop = np.copy(frame[startY:endY, startX:endX]) if (face_crop.shape[0]) < 10 or (face_crop.shape[1]) < 10: continue # preprocessing for gender detection model face_crop = cv2.resize(face_crop, (96, 96)) face_crop = face_crop.astype("float") / 255.0 face_crop = img_to_array(face_crop) face_crop = np.expand_dims(face_crop, axis=0) # apply gender detection on face conf = model.predict(face_crop)[0] print(conf) print(classes) # get label with max accuracy idx = np.argmax(conf) label = classes[idx] label = "{}: {:.2f}%".format(label, conf[idx] * 100) # [ADS source] adsman = [r'D:\ads\man\man1.jpg', r'D:\ads\man\man2.jpg', r'D:\ads\man\man3.jpg'] adswoman = [r'D:\ads\woman\woman1.jpg', r'D:\ads\woman\woman2.jpg', r'D:\ads\woman\woman3.jpg'] if label == 'man': random.shuffle(adsman) path = adsman[0] image = cv2.imread(path) else: random.shuffle(adswoman) path = adswoman[0] image = cv2.imread(path) Y = startY - 10 if startY - 10 > 10 else startY + 10 # write label and confidence above face rectangle cv2.putText(frame, label, (startX, Y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) # display output cv2.imshow("ads.jpg", image) # press "Q" to stop if cv2.waitKey(1) & 0xFF == ord('q'): break # release resources webcam.release() cv2.destroyAllWindows()
Paste Settings
Paste Title :
[Optional]
Paste Folder :
[Optional]
Select
Syntax Highlighting :
[Optional]
Select
Markup
CSS
JavaScript
Bash
C
C#
C++
Java
JSON
Lua
Plaintext
C-like
ABAP
ActionScript
Ada
Apache Configuration
APL
AppleScript
Arduino
ARFF
AsciiDoc
6502 Assembly
ASP.NET (C#)
AutoHotKey
AutoIt
Basic
Batch
Bison
Brainfuck
Bro
CoffeeScript
Clojure
Crystal
Content-Security-Policy
CSS Extras
D
Dart
Diff
Django/Jinja2
Docker
Eiffel
Elixir
Elm
ERB
Erlang
F#
Flow
Fortran
GEDCOM
Gherkin
Git
GLSL
GameMaker Language
Go
GraphQL
Groovy
Haml
Handlebars
Haskell
Haxe
HTTP
HTTP Public-Key-Pins
HTTP Strict-Transport-Security
IchigoJam
Icon
Inform 7
INI
IO
J
Jolie
Julia
Keyman
Kotlin
LaTeX
Less
Liquid
Lisp
LiveScript
LOLCODE
Makefile
Markdown
Markup templating
MATLAB
MEL
Mizar
Monkey
N4JS
NASM
nginx
Nim
Nix
NSIS
Objective-C
OCaml
OpenCL
Oz
PARI/GP
Parser
Pascal
Perl
PHP
PHP Extras
PL/SQL
PowerShell
Processing
Prolog
.properties
Protocol Buffers
Pug
Puppet
Pure
Python
Q (kdb+ database)
Qore
R
React JSX
React TSX
Ren'py
Reason
reST (reStructuredText)
Rip
Roboconf
Ruby
Rust
SAS
Sass (Sass)
Sass (Scss)
Scala
Scheme
Smalltalk
Smarty
SQL
Soy (Closure Template)
Stylus
Swift
TAP
Tcl
Textile
Template Toolkit 2
Twig
TypeScript
VB.Net
Velocity
Verilog
VHDL
vim
Visual Basic
WebAssembly
Wiki markup
Xeora
Xojo (REALbasic)
XQuery
YAML
HTML
Paste Expiration :
[Optional]
Never
Self Destroy
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Status :
[Optional]
Public
Unlisted
Private (members only)
Password :
[Optional]
Description:
[Optional]
Tags:
[Optional]
Encrypt Paste
(
?
)
Create New Paste
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Site Languages
×
English
Tiếng Việt
भारत