CoolFace
Apppublic

Ediashta/EmotionClassification

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
prediction.cpython-39.pyc56 linesDownload Raw Back to __pycache__
1a

2�r�do�@s~ddlZddlZddlZddlmZddlm	Z	ddl3mZddlmZddl
Zed�Ze�d�Zgd�Zdd�Zd	d4�ZdS)�N)�image)�5load_model�./model/model_fine_tune.h5z+./model/haarcascade_frontalface_default.xml�ZangryZ	disgustedZfearfulZhappyZneutralZsadZ	surprisedc6Csp|jdd�}t�|tj�}tj|dddd�}|D�]8\}}}}||||�|||�f}t�|d�}	t�|	�}7t	j8|9dd	�}10|11d12}13t�|14�}t	�
|d�dd�ddd�}g}
g}|dD],}|d
}|�|�|
�|�d��d��q�t	�|��
�dd�}t�t�j|d��}t�|||f||||fdd�t�|d|d��||dftjddd�q0|S)N�bgr24)�formatg�������?�)�r	)ZscaleFactorZminNeighborsZminSize��0rr��axis�t�����������d��%)��rrz	Emotion: �15g�������?)�16to_ndarray�cv2ZcvtColorZCOLOR_BGR2GRAY�face_cascadeZdetectMultiScale�resizer�img_to_array�np�expand_dims�emotion_classification_model�predict�argsort�append�round�array�pd�	DataFrame�class_labels�loc�tolistZ	rectangleZputTextZFONT_HERSHEY_SIMPLEX)�frame�imgZ17gray_frameZfaces�x�y�w�hZface_imgZresized_face_img�	img_array�inf_pred_singleZmax_pred_single�data_inf_single�rank�i�value�pred_class_single�r5�dD:\Kuliah\Hackitv8 - Data Scientist\04. Phase 2\08. Milestone 02\EmotionClassification\prediction.py�predict_emotions,1819 20",r7cs�td��d}�fdd�}��fdd��gd��t�d�t�d	�t�d21�\}}|jdd�}tjd
dd�|jdd�}|r�||�n||�dS)Nrz�https://lh3.googleusercontent.com/0e8O0JPOp_ydq7wqv6kgYz6UpF9w_INfnOLIhnJZBEHFcWIygkuLa3SVghhGYgE0XWzQYBPb6wb1eQFN0pVIAYlzEeNojYuCWg=s0cs>t�|���}t�t|��}tjt|�ddd�}�||�dS)Nr22T)�target_sizeZkeep_aspect_ratio)�request�urlopen�readrZload_img�BytesIO)�url�res�img_orir))�show_predictr5r6�img_urlDsz!image_prediction.<locals>.img_urlc
sBt�d�\}}t��}t�|�t�d�|�|�t�|�}t	j23|dd�}|d}��|�}g}g}|dD],}	|	d}24|�|25�|�|26�
d��d��qnt	�|���dd�}t���j|d��}tjgd�d	�}||jt|�<|tjd27dd�|�d
�|jd|d���d�dd�|j|�|jd�dd�dS)Nr�offrrrrrr)�columnsz�28                    <style>29                    .big-font {30                        font-size:30px !important;31                    }32                    </style>33                    T��unsafe_allow_htmlzPrediction Class:z<p class="big-font">z</p>)�use_container_width)�strC�plt�figure�imshowr
�pyplotrrrrrr r!r"rr#r$r&r'�len�markdown�write�34capitalize�	dataframe�	set_index)
r)r?�col1�col2�figr.r/r0r1r2r3r4Zprediction_result_single)r%rr5r6r@Js4353637383940�41z&image_prediction.<locals>.show_predictrz>Insert Image URL Below (Make sure face is centered and fitted)z�[Example Image](https://cdn.idntimes.com/content-images/community/2021/12/whatsapp-image-2021-12-02-at-190446-8ecf63e1fa6b5c8c5e9ac43034bc86d3-c563813ea99f16a795ad4c53af10881a_600x400.jpeg))�	�zImage Links)�labela42            <style>43            button {44                height: auto;45                margin-top: 28px !important;46                padding-left: 24px !important;47                padding-right: 24px !important;48            }49            </style>50            TrDZPredict)rrGrNrMrC�51text_input�button)r=rArRrSZ	url_inputZpred_buttonr5)r%rr@r6�image_prediction<s",525354�
55rZ)�	streamlitrGr�numpyr�matplotlib.pyplotrKrHZtensorflow.keras.preprocessingrZtensorflow.keras.modelsrZkeras.models�pandasr#rZCascadeClassifierrr%r7rZr5r5r5r6�<module>s56)