Skip to content

Instantly share code, notes, and snippets.

View shubham0204's full-sized avatar
🎯
Focusing

Shubham Panchal shubham0204

🎯
Focusing
View GitHub Profile
// ML Model to detect the age of a person, hosted on Heroku
class AgeDetectionModel {
private val herokuModelPredictURL = "https://age-detection-tf-app.herokuapp.com/predict"
private val mediaType = "application/json".toMediaType()
private val okHttpClient = OkHttpClient()
interface PredictionCallback {
fun onResult( age : Int )
fun onError( error : String )
// Detect faces in the given image and crop them.
// Pass the cropped faces to the AgeDetectionModel
private fun detectFaces(image: Bitmap) {
val inputImage = InputImage.fromBitmap(image, 0)
firebaseFaceDetector.process(inputImage)
.addOnSuccessListener { faces ->
if ( faces.size != 0 ) {
progressDialog.apply {
dismiss()
setMessage( "📍 Posting to server ...")
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
// 1. MLKit Face Detection
implementation 'com.google.android.gms:play-services-mlkit-face-detection:17.0.0'
// 2. OkHttp is a HTTP client for Android
private lateinit var activityMainBinding : ActivityMainBinding
private lateinit var progressDialog : ProgressDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = ActivityMainBinding.inflate( layoutInflater )
setContentView( activityMainBinding.root )
progressDialog = ProgressDialog( this )
from flask import Flask , jsonify , request
from PIL import Image
import tensorflow as tf
import numpy as np
import base64
import io
# Loading the Keras model to perform inference
# Download the model from this release ->
# https://github.com/shubham0204/Age-Gender_Estimation_TF-Android/releases/tag/v1.0
import math
import matplotlib.pyplot as plt
def poisson( lambda_ , x ):
return ( (lambda_)**x * math.exp(-lambda_) ) / math.factorial(x)
x = []
y = []
for i in range( 100 ):
x.append( i )
hidden_dims = 128
token_mixing_mlp_dims = 64
channel_mixing_mlp_dims = 128
patch_size = 9
num_classes = 10
num_mixer_layers = 4
input_image_shape = ( 32 , 32 , 3 )
inputs = tf.keras.layers.Input( shape=input_image_shape )
hidden_dims = 128
token_mixing_mlp_dims = 64
channel_mixing_mlp_dims = 128
patch_size = 9
num_classes = 10
num_mixer_layers = 4
reshape_image_dim = 72
input_image_shape = ( 32 , 32 , 3 )
inputs = tf.keras.layers.Input( shape=input_image_shape )
# Mixer layer consisting of token mixing MLPs and channel mixing MLPs
# input shape -> ( batch_size , channels , num_patches )
# output shape -> ( batch_size , channels , num_patches )
def mixer( x , token_mixing_mlp_dims , channel_mixing_mlp_dims ):
# inputs x of are of shape ( batch_size , num_patches , channels )
# Note: "channels" is used instead of "embedding_dims"
# Add token mixing MLPs
token_mixing_out = token_mixing( x , token_mixing_mlp_dims )
# Shape of token_mixing_out -> ( batch_size , channels , num_patches )