-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi.py
55 lines (51 loc) · 2.53 KB
/
api.py
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
import io
import requests,json
from PIL import Image
import numpy as np
from picamera import PiCamera
from picamera.array import PiRGBArray
from time import sleep
#Creating an instance for the pi camera
camera = PiCamera(resolution=(100,100))
#camera.start_preview()
#sleep(5)
while True:
with PiRGBArray(camera, size=(100,100)) as output:
camera.capture(output,'rgb')
arr=output.array.tolist()
payload = { "values" : [arr] }
#print(payload)
# NOTE: Get the ml_instance id from service credentials of machine learning instance
ml_instance_id="084b11f3-c072-4d12-b7a3-ec806f3500e3"
# NOTE: Generate iam_token based on apikey
# another apikey=<jGfi97h047faAAf7SBsGm6cAAFu_KbisQIX2IUCFqmE->
print("Generating IAM Token")
url="https://iam.cloud.ibm.com/identity/token"
iam_header={'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'}
data={"grant_type":"urn:ibm:params:oauth:grant-type:apikey", "apikey":"Ws-Y6SW7YIGlaM13N2HY2_hkoC1JuvuWYmpNqQYrjFhT"}
resp = requests.post(url=url, data=data, headers=iam_header)
iam_token=resp.json()["access_token"]
iam_token='Bearer '+iam_token
print("Received token is : ",iam_token)
# NOTE: generate iam_token and retrieve ml_instance_id based on provided documentation
header ={'Content-Type' :'application/json', 'Authorization': iam_token, 'ML-Instance-ID': ml_instance_id}
response_scoring = requests.post('https://us-south.ml.cloud.ibm.com/v3/wml_instances/084b11f3-c072-4d12-b7a3-ec806f3500e3/deployments/fa86647a-737b-4a93-aa5c-0da9957a32c4/online', json=payload, headers=header)
print("Scoring response")
response=json.loads(response_scoring.text)
print(response)
response=response['values']
prediction=np.argmax(response)
print(prediction)
# Class 1 is mobile and Class 0 is not
predicted_class = 'Mobile' if prediction==1 else 'Not Mobile'
print(predicted_class)
output.truncate(0)
sleep(0.2)
if prediction == 1:
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print("WARNING: MOBILE SPOOFING DETECTED!!!!")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
#camera.stop_preview()
break