CoolFace
Apppublic

Akashc6/aathraos-mobility

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
api.cpython-312.pyc35 linesDownload Raw Back to __pycache__
12�i�����UddlmZmZmZddlmZddlZddlZddlm	Z	ddl3mZddlm
Z
edd�	�Zdae	eed4<dae	ej$ed<Gd�d
e�ZGd�de�Zdedefd�Zej/ddg��defd��Zej/ddg��d��Zej5dedg��d��Zej5ddg��d��Zed k(rddlZej>d!d"d#d$�%�yy)&�)�FastAPI�BackgroundTasks�
HTTPException)�	BaseModelN)�Optional)�TrafficCounter)�
PeakPredictorz'Traffic & Crowd Real-time Analytics APIzIAPI to control and query object tracking models for real-time CCTV feeds.)�title�description�active_tracker�tracker_threadc�&�eZdZUeed<dZeed<y)�StreamStartRequest�urlzdata/live_api_traffic_log.csv�5output_logN)�__name__�6__module__�__qualname__�str�__annotations__r���A/media/akash/New_Volume/amd sling/traffic_crowd_prediction/api.pyrrs��	�H�5�J��5rrc�V�eZdZUeed<dZeeed<dZeed<dZ	eed<dZ7eed<y)�TrafficStatusResponse�statusN�
active_streamr�persons�vehicles�total)rrrrrrrr�intrr rrrrrs2���K�#'�M�8�C�=�'��G�S���H�c���E�3�Nrrr�8output_csvc���	t||��atd|���tjd��day#t$r}td|���Yd}~dayd}~wwxYw#dawxYw)N)�9video_pathr"z'Starting Background Tracker on Stream: F)�10show_videozError in background tracker: )rr�print�
process_video�	Exception)rr"�es   r�run_tracker_in_backgroundr*sm���'�3�:�N��
�7��u�=�>��$�$��$�6�����3�
�-�a�S�1�2�2����3����s&�16�	A�A�
A�A�A�A z
/stream/startzCCTV Stream Management)�tags�requestc��FK�tr@tjr0tj�trtj	d��tjt|j|jfd��atj�dd|j�d�iS�w)z�11    Starts the video tracking background process for a given RTSP/HTTP URL.12    If a stream is already running, stops it and starts the new one.13    g@)�timeoutT)�target�args�daemon�messagez"Successfully launched tracker for z in background)r�14is_running�stopr
�join�	threading�Threadr*rr�start)r,s r�start_streamr9*s������.�3�3�����������,��%�%�(��k�k�7�-�-�
.���N�15�����;�G�K�K�=��W�X�X�s�BB!z/stream/stopc��nK�tr(tjrtj�ddiSddiS�w)z>16    Stops the currently running video processing stream.17    r2zActive stream tracking stopped.zNo stream currently tracking.)rr3r4rrr�stop_streamr;Bs7�����.�3�3������<�=�=��6�7�7�s�35z/stream/status�	Analytics)�response_modelr+c��BK�tr�tjrztj}tdt	tj18�|j
dd�|j
dd�|j
dd�|j
dd�z��Std��S�w)zc19    Retrieves the actual live counting metrics from the actively running CCTV stream tracker.20    �running�personr�vehicle)rrrrr �idle)r)rr3�countsrrr$�get)rCs r�get_stream_statusrEMs������.�3�3��&�&��$���n�7�7�8��J�J�x��+��Z�Z�	�1�-��*�*�X�q�)�F�J�J�y�!�,D�D�21�	22�!��/�/�s�BBz/forecast/peaksc��TK�tdd��}|j�[|j�sKtjjd�r|j
�}|stdd���tdd���|j�}|�
tdd	���|jd23��S�w)zp24    Retrieves the predicted weekly peak hour timings from our ML model,25    using historical traffic data.26    zdata/historical_traffic_log.csvzmodels/rf_model.pkl)�	data_path�27model_pathi�z4Failed to train model from existing historical data.)�status_code�detaili�zfHistorical data not found. Run generate_synthetic_data.py to collect base patterns before forecasting.zPeak prediction returned None�records)�orient)28r	�model�29load_model�os�path�exists�train_modelr�predict_peak_timings�to_dict)�	predictor�success�df_peakss   r�get_peak_forecastrX`s������(I�Vk�l�I�����y�';�';�'=�
�7�7�>�>�;�<��+�+�-�G��"�s�;q�r�r�����
�30�-�-�/�H�����4S�T�T����9��-�-�s�B&B(�__main__zapi:appz0.0.0.0i@T)�host�port�reload) �fastapirrr�pydanticrr6rO�typingr�31cv_counterr�peak_predictorr	�apprrr
r7rrrr*�postr9r;rDrErXr�uvicorn�runrrr�<module>rfsE��;�;���	��%�(�
�323�[���,0����(�/�-1����)�)�*�1�6��6��I��33�3�34�C�35����/�!9� :��;�Y� 2�Y�<�Y�.���.� 8�9��:�8�;�8����	�*?�{�m��T�0�U�0�$���	�+���/�.�0�.�4�z����G�K�K�	�	��T�B�r