Exemple #1
0
def detect_json_array_to_new_message_array(fn):    
    with open(fn, encoding='utf8') as f:
        jsonArray = json.load(f)
    
    returnArray = []
    for jsonObject in jsonArray:
        message_type = jsonObject.get('type')
        if message_type == 'text':
            returnArray.append(TextSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'imagemap':
            returnArray.append(ImagemapSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'template':
            returnArray.append(TemplateSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'image':
            returnArray.append(ImageSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'sticker':
            returnArray.append(StickerSendMessage.new_from_json_dict(jsonObject))  
        elif message_type == 'audio':
            returnArray.append(AudioSendMessage.new_from_json_dict(jsonObject))  
        elif message_type == 'location':
            returnArray.append(LocationSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'flex':
            returnArray.append(FlexSendMessage.new_from_json_dict(jsonObject))    

    return returnArray
Exemple #2
0
def detect_json_array_to_new_message_array(fileName):
    # 開啟檔案,轉成json
    with open(fileName, 'r', encoding="utf-8") as f:
        jsonArray = json.load(f)

    # 解析json
    returnArray = []
    for jsonObject in jsonArray:

        # 讀取其用來判斷的元件
        message_type = jsonObject.get('type')

        # 轉換
        if message_type == 'text':
            returnArray.append(TextSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'imagemap':
            returnArray.append(ImagemapSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'template':
            returnArray.append(TemplateSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'image':
            returnArray.append(ImageSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'sticker':
            returnArray.append(StickerSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'audio':
            returnArray.append(AudioSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'location':
            returnArray.append(LocationSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'flex':
            returnArray.append(FlexSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'video':
            returnArray.append(FlexSendMessage.new_from_json_dict(jsonObject))

            # 回傳
    return returnArray
Exemple #3
0
def detect_json_array_to_new_message_array(fileName):
    ''' message_type 判斷器

        讀取指定的 json檔,解析成不同格式的 SendMessage
    '''
    with open(fileName, 'r', encoding='utf8') as f:
        jsonArray = json.load(f)
    
    newmessage_Array = []
    for jsonObject in jsonArray:
        message_type = jsonObject.get('type')
        
        if message_type == 'text':
            newmessage_Array.append(TextSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'sticker':
            newmessage_Array.append(StickerSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'image':
            newmessage_Array.append(ImageSendMessage.new_from_json_dict(jsonObject))  
        elif message_type == 'video':
            newmessage_Array.append(VideoSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'audio':
            newmessage_Array.append(AudioSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'location':
            newmessage_Array.append(LocationSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'imagemap':
            newmessage_Array.append(ImagemapSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'template':
            newmessage_Array.append(TemplateSendMessage.new_from_json_dict(jsonObject))
        elif message_type == 'flex':
            newmessage_Array.append(FlexSendMessage.new_from_json_dict(jsonObject))        

    return newmessage_Array
Exemple #4
0
def handle_message(event):
    message_content = line_bot_api.get_message_content(event.message.id)

    # 開始臉部辨識
    detected_faces = face_client.face.detect_with_stream(
        BytesIO(message_content.content))

    if not detected_faces:
        line_bot_api.reply_message(event.reply_token,
                                   TextSendMessage(text="找不到臉...."))
        raise Exception('No face detected from image...')

    # Convert width height to a point in a rectangle
    def getRectangle(faceDictionary):
        rect = faceDictionary.face_rectangle
        left = rect.left
        top = rect.top
        right = left + rect.width
        bottom = top + rect.height

        return ((left, top), (right, bottom))

    img = Image.open(BytesIO(message_content.content))

    # For each face returned use the face rectangle and draw a red box.
    print('Drawing rectangle around face...')
    draw = ImageDraw.Draw(img)
    for face in detected_faces:
        draw.rectangle(getRectangle(face), outline='red')

    # Display the image in the users default image browser.
    # img.show()
    # Save the image from users.
    img.save(f'./material/detected_images/{event.message.id}.jpg')

    # 準備要回傳的圖片
    base_url = secretFileContentJson.get('base_url')
    detected_image_message_dict = {
        "type":
        "image",
        "originalContentUrl":
        urljoin(base_url, f'material/detected_images/{event.message.id}.jpg'),
        "previewImageUrl":
        urljoin(base_url, f'material/detected_images/{event.message.id}.jpg')
    }

    line_bot_api.reply_message(
        event.reply_token,
        ImageSendMessage.new_from_json_dict(detected_image_message_dict))