- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
Latest commit
76 lines (58 loc) · 2.71 KB
/
Copy pathutils.py
File metadata and controls
76 lines (58 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
fromclarifai_grpc.channel.clarifai_channelimportClarifaiChannel
fromclarifai_grpc.grpc.apiimportservice_pb2_grpc, service_pb2, resources_pb2
fromclarifai_grpc.grpc.api.statusimportstatus_code_pb2
fromrandomimportrandint
fromtelegramimportReplyKeyboardMarkup, KeyboardButton, InlineKeyboardButton, InlineKeyboardMarkup
importsettings
defget_bot_number(user_number):
returnrandint(user_number-10, user_number+10)
defplay_random_numbers(user_number, bot_number):
ifuser_number>bot_number:
message=f"Ваше число {user_number}, мое {bot_number}, вы выиграли"
elifuser_number==bot_number:
message=f"Ваше число {user_number}, мое {bot_number}, ничья"
else:
message=f"Ваше число {user_number}, мое {bot_number}, вы проиграли"
returnmessage
defmain_keyboard():
returnReplyKeyboardMarkup([
['Прислать котика', KeyboardButton('Мои координаты', request_location=True), 'Заполнить анкету']
])
defhas_object_on_image(file_name, object_name):
channel=ClarifaiChannel.get_grpc_channel()
app=service_pb2_grpc.V2Stub(channel)
metadata= (('authorization', f'Key {settings.CLARIFAI_API_KEY}'),)
withopen(file_name, 'rb') asf:
file_data=f.read()
image=resources_pb2.Image(base64=file_data)
request=service_pb2.PostModelOutputsRequest(
model_id='aaa03c23b3724a16a56b629203edc62c',
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(image=image)
)
])
response=app.PostModelOutputs(request, metadata=metadata)
# print(response)
returncheck_response_for_object(response, object_name)
defcheck_response_for_object(response, object_name):
ifresponse.status.code==status_code_pb2.SUCCESS:
forconceptinresponse.outputs[0].data.concepts:
ifconcept.name==object_nameandconcept.value>=0.9:
returnTrue
else:
print(f'Ошибка распознования картинки {response.outputs[0].status.details}')
returnFalse
defcat_rating_inline_keyboard(image_name):
callback_text=f"rating|{image_name}|"
keyboard= [
[
InlineKeyboardButton('Нравится', callback_data=callback_text+'1'),
InlineKeyboardButton('Не нравится', callback_data=callback_text+'-1')
]
]
returnInlineKeyboardMarkup(keyboard)
if__name__=='__main__':
print(has_object_on_image('images/cat1.jpg', 'cat'))
print(has_object_on_image('images/not_cat.jpg', 'cat'))
print(has_object_on_image('images/not_cat2.jpg', 'dog'))