detect_label.ipynb 30 KB

# %pip install awswrangler
# %conda install -c conda-forge ffmpeg
# %pip install av
import awswrangler as wr
import pandas as pd
import numpy as np
import boto3, io
# from sagemaker import get_execution_role
import av
import numpy as np
from pathlib import Path
import datetime
import string
import random

# role = get_execution_role()
session = boto3.Session(profile_name='homeiot')
# session = boto3.Session(profile_name='danji')
rekognition = session.client('rekognition')
def detect_labels(img):
    response = rekognition.detect_labels(Image={'Bytes': img})
    for label in response['Labels']:
        if (label['Name'] in ['Human', 'Person']) and label['Confidence'] > 90:
            return True
    return False

def detect_time(img):
    target_datetime = 'not detected'
    response = rekognition.detect_text(Image={'Bytes': img})
    for text in response['TextDetections']:
        if abs(text['Geometry']['BoundingBox']['Left'] - 0.05218505859375) < 0.003 and abs(text['Geometry']['BoundingBox']['Width'] - 0.35760498046875) < 0.01 :
            target_datetime = text['DetectedText']    
    return target_datetime
def detect_obj(filepath):
    container = av.open('video/2-3회의실/20210823_081041A.mp4')
    stream = container.streams.video[0]
    # stream.codec_context.skip_frame = 'NONKEY'
    mat = []
    for frame in container.decode(stream):
        img_byte_array = io.BytesIO()
        img = frame.to_image()
        img.save(img_byte_array, format='JPEG', subsampling=0, quality=100)
        is_label = detect_labels(img_byte_array.getvalue())
        t = detect_time(img_byte_array.getvalue())
        mat.append( (t, is_label) )
        print(f'{t}-{is_label}')
    return mat
import os.path as pt
import glob
files = glob.glob('video/2-3회의실/*.mp4')
for f in files:
    print(f'proc {f}')
    mat = detect_obj(f)
    pd.DataFrame(mat).to_csv(pt.split(f)[0]+'.csv')
proc video/2-3회의실/20210823_081041A.mp4
2021-08-23 08:10:41-False
2021-08-23 08:11:41-False
2021-08-23 08:12:41-False
2021-08-23 08:13:41-False
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-6-89447fc32afb> in <module>
      4 for f in files:
      5     print(f'proc {f}')
----> 6     mat = detect_obj(f)
      7     pd.DataFrame(mat).to_csv(pt.split(f)[0]+'.csv')

<ipython-input-3-04832645884d> in detect_obj(filepath)
      9         img.save(img_byte_array, format='JPEG', subsampling=0, quality=100)
     10         is_label = detect_labels(img_byte_array.getvalue())
---> 11         t = detect_time(img_byte_array.getvalue())
     12         mat.append( (t, is_label) )
     13         print(f'{t}-{is_label}')

<ipython-input-2-762166df9460> in detect_time(img)
      8 def detect_time(img):
      9     target_datetime = 'not detected'
---> 10     response = rekognition.detect_text(Image={'Bytes': img})
     11     for text in response['TextDetections']:
     12         if abs(text['Geometry']['BoundingBox']['Left'] - 0.05218505859375) < 0.003 and abs(text['Geometry']['BoundingBox']['Width'] - 0.35760498046875) < 0.01 :

~/.local/lib/python3.8/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
    389                     "%s() only accepts keyword arguments." % py_operation_name)
    390             # The "self" in this scope is referring to the BaseClient.
--> 391             return self._make_api_call(operation_name, kwargs)
    392 
    393         _api_call.__name__ = str(py_operation_name)

~/.local/lib/python3.8/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
    703             http, parsed_response = event_response
    704         else:
--> 705             http, parsed_response = self._make_request(
    706                 operation_model, request_dict, request_context)
    707 

~/.local/lib/python3.8/site-packages/botocore/client.py in _make_request(self, operation_model, request_dict, request_context)
    723     def _make_request(self, operation_model, request_dict, request_context):
    724         try:
--> 725             return self._endpoint.make_request(operation_model, request_dict)
    726         except Exception as e:
    727             self.meta.events.emit(

~/.local/lib/python3.8/site-packages/botocore/endpoint.py in make_request(self, operation_model, request_dict)
    100         logger.debug("Making request for %s with params: %s",
    101                      operation_model, request_dict)
--> 102         return self._send_request(request_dict, operation_model)
    103 
    104     def create_request(self, params, operation_model=None):

~/.local/lib/python3.8/site-packages/botocore/endpoint.py in _send_request(self, request_dict, operation_model)
    132         request = self.create_request(request_dict, operation_model)
    133         context = request_dict['context']
--> 134         success_response, exception = self._get_response(
    135             request, operation_model, context)
    136         while self._needs_retry(attempts, operation_model, request_dict,

~/.local/lib/python3.8/site-packages/botocore/endpoint.py in _get_response(self, request, operation_model, context)
    163         # If an exception occurs then the success_response is None.
    164         # If no exception occurs then exception is None.
--> 165         success_response, exception = self._do_get_response(
    166             request, operation_model)
    167         kwargs_to_emit = {

~/.local/lib/python3.8/site-packages/botocore/endpoint.py in _do_get_response(self, request, operation_model)
    197             http_response = first_non_none_response(responses)
    198             if http_response is None:
--> 199                 http_response = self._send(request)
    200         except HTTPClientError as e:
    201             return (None, e)

~/.local/lib/python3.8/site-packages/botocore/endpoint.py in _send(self, request)
    266 
    267     def _send(self, request):
--> 268         return self.http_session.send(request)
    269 
    270 

~/.local/lib/python3.8/site-packages/botocore/httpsession.py in send(self, request)
    383 
    384             request_target = self._get_request_target(request.url, proxy_url)
--> 385             urllib_response = conn.urlopen(
    386                 method=request.method,
    387                 url=request_target,

~/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    697 
    698             # Make the request on the httplib connection object.
--> 699             httplib_response = self._make_request(
    700                 conn,
    701                 method,

~/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    443                     # Python 3 (including for exceptions like SystemExit).
    444                     # Otherwise it looks like a bug in the code.
--> 445                     six.raise_from(e, None)
    446         except (SocketTimeout, BaseSSLError, SocketError) as e:
    447             self._raise_timeout(err=e, url=url, timeout_value=read_timeout)

~/anaconda3/lib/python3.8/site-packages/urllib3/packages/six.py in raise_from(value, from_value)

~/anaconda3/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    438                 # Python 3
    439                 try:
--> 440                     httplib_response = conn.getresponse()
    441                 except BaseException as e:
    442                     # Remove the TypeError from the exception chain in

~/anaconda3/lib/python3.8/http/client.py in getresponse(self)
   1345         try:
   1346             try:
-> 1347                 response.begin()
   1348             except ConnectionError:
   1349                 self.close()

~/anaconda3/lib/python3.8/http/client.py in begin(self)
    305         # read until we get a non-100 response
    306         while True:
--> 307             version, status, reason = self._read_status()
    308             if status != CONTINUE:
    309                 break

~/anaconda3/lib/python3.8/http/client.py in _read_status(self)
    266 
    267     def _read_status(self):
--> 268         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    269         if len(line) > _MAXLINE:
    270             raise LineTooLong("status line")

~/anaconda3/lib/python3.8/socket.py in readinto(self, b)
    667         while True:
    668             try:
--> 669                 return self._sock.recv_into(b)
    670             except timeout:
    671                 self._timeout_occurred = True

~/anaconda3/lib/python3.8/ssl.py in recv_into(self, buffer, nbytes, flags)
   1239                   "non-zero flags not allowed in calls to recv_into() on %s" %
   1240                   self.__class__)
-> 1241             return self.read(nbytes, buffer)
   1242         else:
   1243             return super().recv_into(buffer, nbytes, flags)

~/anaconda3/lib/python3.8/ssl.py in read(self, len, buffer)
   1097         try:
   1098             if buffer is not None:
-> 1099                 return self._sslobj.read(len, buffer)
   1100             else:
   1101                 return self._sslobj.read(len)

KeyboardInterrupt: