Exemple #1
0
async def drink_or_not_drink(image_url, message):
    """Checks to see if an image is a drink"""
    app = ClarifaiApp(api_key=clarifai_api_key)
    message_channel = message.channel
    model = app.models.get('general-v1.3')
    image = Image(url=image_url)
    response_data = model.predict([image])

    concepts = response_data['outputs'][0]['data']['concepts']

    drinks = [
        'beer', 'alcohol', 'cocktail',
        'wine', 'liquor', 'martini',
        'vodka', 'whisky', 'bourbon',
        'Scotch'
    ]

    for concept in concepts:
        for drink in drinks:
            if concept['name'] == drink and concept['value'] > 0.92:
                await bot.send_message(
                    message_channel,
                    'Problem drinking'
                    ' hyperdrive detection algorithm (PDHDA) has detected '
                    'an adult beverage. Good work, Bro!')
                message.content = "!drink"
                return
Exemple #2
0
  def test_predict_model(self):
    """ test predict with general model """

    # model = self.app.models.get(model_id=GENERAL_MODEL_ID)
    model = self.app.models.get(model_id='aaa03c23b3724a16a56b629203edc62c')
    image = Image(url=sample_inputs.METRO_IMAGE_URL)

    res = model.predict([image])
    self.assertEqual(10000, res['status']['code'])
    def test_predict_model(self):
        """ test predict with general model """

        # model = self.app.models.get('general-v1.3')
        model = self.app.models.get(
            model_id='aaa03c23b3724a16a56b629203edc62c')
        image = Image(url=urls[0])

        res = model.predict([image])
        self.assertEqual(10000, res['status']['code'])
Exemple #4
0
def main_app():
    """Get images of the logged in user."""
    response = requests.get(MEDIA_URL.format(session.get('insta_auth')))
    images = [image.get('images') for image in response.json().get('data')]
    urls = map(lambda x: x.get('standard_resolution').get('url'), images)
    base_64_contents = map(lambda url: Image(url=url), urls)
    capp = ClarifaiApp(api_key=CLARIFAI_APP_ID)
    model = capp.models.get('general-v1.3')
    output = model.predict(base_64_contents)
    results = map(whip, output.get('outputs'))
    return render_template('app.html', response=results)
Exemple #5
0
    def post(self):
        app = ClarifaiApp(api_key="be355c0550a94e6282fb8adcb83f6a49")
        model = app.models.get(DATASET)

        file = self.request.FILES[0]
        image = Image(filename=file)

        predictions = model.predict([image])
        concepts = predictions["outputs"][0]["data"]["concepts"]

        predictions = []
        for product in concepts:
            name, value = concepts["name"], concepts["value"]
            predictions.append((name, value))
Exemple #6
0
def get_clarifai_app_object(urls):
    """
    Initialize indexing of images and return clarifai api object
    :param urls: List
    :return: ClarifaiApp
    """
    c_app = ClarifaiApp(CLARIFAI_KEY, CLARIFAI_SECRET)
    for arrays in chunks(urls, 128):
        try:
            c_app.inputs.bulk_create_images([
                Image(url=url, image_id=md5(url).hexdigest()) for url in arrays
            ])
        except:
            print "Trying to index duplicate images"
    return c_app
Exemple #7
0
    def get(self, image_url):

        from clarifai.rest import ClarifaiApp, Image

        app = ClarifaiApp(api_key='2a018a8bc2e443f9912f4f962c1a4653')

        model = app.models.get('food-items-v1.0')
        image = Image(url="https://samples.clarifai.com/" + image_url + ".jpg")
        result_items = model.predict([image])['outputs'][0]['data']['concepts']
        result_list = []
        for tag in range(len(result_items)):
            result_item = result_items[tag]['name']
            result_list.append(result_item)

        return {"message": result_list}
Exemple #8
0
def img_has_cat(filename):
    app = ClarifaiApp(api_key=settings.CLARIFAI_API_KEY)
    model = app.models.get("general-v1.3")
    try:
        image = Image(file_obj=open(filename, 'rb'))
        result = model.predict([image])
        try:
            items = result['outputs'][0]['data']['concepts']
            for item in items:
                if item['name'] == 'cat':
                    return True
            else:
                return False
        except (IndexError):
            return False
    except (client.ApiError, FileNotFoundError):
        return False
Exemple #9
0
def image():
    form = ReusableForm(request.form)
    if request.method == 'POST':
        image_url = request.form['image']

        img_model = cli.models.get('Galaxy merger Propertiesxc')
        image = Image(file_obj=open(image_url, 'rb'))
        response = img_model.predict([image])

        if form.validate():
            concepts = response['outputs'][0]['data']['concepts']
            for concept in concepts:
                flash((concept['name'], concept['value']))
        else:
            flash('Error: All the form fields are required. ')

    return render_template('image.html', form=form)
Exemple #10
0
    def test_predict_multiple_times_single_image(self):
        """ construct one image object and predict multiple times """
        model = self.app.models.get('general-v1.3')

        # predict by url
        f = tempfile.NamedTemporaryFile(delete=False)
        filename = f.name
        raw_bytes = self.app.api.session.get(urls[0]).content
        f.write(raw_bytes)
        f.close()

        img = Image(filename=filename)

        res = model.predict([img, img])
        res = model.predict([img, img])

        os.unlink(filename)
Exemple #11
0
def img_has_cat(filename):  #url):
    app = ClarifaiApp(api_key="ee15b61906ee4423b5c9bea34977228d")
    model = app.models.get("general-v1.3")

    try:
        image = Image(file_obj=open(filename, 'rb'))
        #result = model.predict_by_url(url=url)
        result = model.predict([image])
        try:
            items = result['outputs'][0]['data']['concepts']
            for item in items:
                if item['name'] == 'cat':
                    return True
            else:
                return False
        except (IndexError):
            return False
    except (client.ApiError):
        return False
Exemple #12
0
def model():
    imgUrl = request.args.get('imgUrl')

    tags = api.predictModel(model_id=model_id, objs=[Image(url=imgUrl)])

    ##Debug: print json.dumps(tags, indent=4, sort_keys=True)
    value_dict = {}
    for tag in tags['outputs'][0]['data']['tags']:
        id = tag['concept']['id']
        value = tag['value']
        value_dict[id] = value

    routine = get_routine(value_dict)

    #Debug:  print "We are showing the value dict"
    #Debug:  print value_dict
    #Debug: routine = getRoutine(tags['tags']['id']
    #Debug: print json.dumps(tags , indent=4, sort_keys=True)

    return json.dumps(routine)
Exemple #13
0
  def test_predict_multiple_times_single_image(self):
    """ construct one image object and predict multiple times """
    model = self.app.models.get(model_id=GENERAL_MODEL_ID)

    # predict by url
    f = tempfile.NamedTemporaryFile(delete=False)
    filename = f.name
    raw_bytes = self.app.api.session.get(sample_inputs.METRO_IMAGE_URL).content
    f.write(raw_bytes)
    f.close()

    img = Image(filename=filename)

    res = model.predict([img, img])
    self.assertEqual(10000, res['status']['code'])

    res = model.predict([img, img])
    self.assertEqual(10000, res['status']['code'])

    os.unlink(filename)
Exemple #14
0
def call_clarifai_url(url, desc='default description'):
    urls = [{'desc': desc, 'url': url}]
    faceiamgeurl = None
    url_list = [url]
    try:
        faceimageurl = FaceImageUrls.get(url)
        if DEBUG: print('get succeeded. already have identification.')
    except:
        if DEBUG: print('get failed. calling clarifai for url', url)

    print('Calling clarifai predict...')
    res1 = workflow.predict([Image(url)])
    res2 = [identify1(_res) for _res in res1['results']]
    res3 = dict(zip(url_list, res2))
    #save urls
    for urlobj in urls:
        url = urlobj['url']
        try:
            faceimageurl = FaceImageUrls.get(url)
            faceimageurl.identification = None
        except:
            faceimageurl = FaceImageUrls(url)
            faceimageurl.numid = FaceImageUrls.count() + 1
            if DEBUG:
                print('creating new FaceImageUrl: {} {} {}'.format(
                    faceimageurl.numid, faceimageurl.description,
                    faceimageurl.url))
        faceimageurl.identification = res3[url]
        if desc == 'default description':
            #dont save
            pass
        else:
            faceimageurl.description = desc
            faceimageurl.save()

    return faceimageurl
def test_predict_with_input_id(mock_http_client):  # type: (mock.Mock) -> None
    mock_execute_request = mock_request(
        mock_http_client, """
{
  "status": {
    "code": 10000, 
    "description": "Ok"
  }, 
  "outputs": [
    {
      "status": {
        "code": 10000, 
        "description": "Ok"
      }, 
      "created_at": "2019-02-27T16:36:12.896173378Z", 
      "input": {
        "data": {
          "image": {
            "url": "@imageURL"
          }
        }, 
        "id": "@inputID"
      }, 
      "model": {
        "display_name": "NSFW", 
        "name": "nsfw-v1.0", 
        "output_info": {
          "type_ext": "concept", 
          "message": "Show output_info with: GET /models/{model_id}/output_info", 
          "type": "concept"
        }, 
        "created_at": "2016-09-17T22:18:59.955626Z", 
        "app_id": "main", 
        "model_version": {
          "status": {
            "code": 21100, 
            "description": "Model trained successfully"
          }, 
          "created_at": "2018-01-23T19:25:09.618692Z", 
          "id": "aa47919c9a8d4d94bfa283121281bcc4", 
          "train_stats": {}
        }, 
        "id": "e9576d86d2004ed1a38ba0cf39ecb4b1"
      }, 
      "data": {
        "concepts": [
          {
            "app_id": "main", 
            "id": "@conceptID1", 
            "value": 0.87529075, 
            "name": "sfw"
          }, 
          {
            "app_id": "main", 
            "id": "@conceptID2", 
            "value": 0.124709226, 
            "name": "nsfw"
          }
        ]
      }, 
      "id": "59fa1efec39244c98b6827694db555e3"
    }
  ]
}
""")

    app = ClarifaiApp()
    model = app.models.get(model_id='@modelID')
    response = model.predict([Image(url='@imageURL', image_id='@inputID')])

    assert_request(
        mock_execute_request, 'POST', '/v2/models/@modelID/outputs', """
{
  "inputs": [
    {
      "data": {
        "image": {
          "url": "@imageURL"
        }
      }, 
      "id": "@inputID"
    }
  ]
}
  """)

    output = response['outputs'][0]
    assert output['input']['id'] == '@inputID'
    assert output['data']['concepts'][0]['id'] == '@conceptID1'
def test_concept_bulk_predict_with_arguments(
        mock_http_client):  # type: (mock.Mock) -> None
    mock_execute_request = mock_request(
        mock_http_client, """
{
  "status": {
    "code": 10000,
    "description": "Ok"
  },
  "outputs": [
    {
      "id": "@outputID1",
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "created_at": "2019-01-29T16:45:43.793810775Z",
      "model": {
        "id": "aaa03c23b3724a16a56b629203edc62c",
        "name": "general",
        "created_at": "2016-03-09T17:11:39.608845Z",
        "app_id": "main",
        "output_info": {
          "message": "Show output_info with: GET /models/{model_id}/output_info",
          "type": "concept",
          "type_ext": "concept"
        },
        "model_version": {
          "id": "aa9ca48295b37401f8af92ad1af0d91d",
          "created_at": "2016-07-13T01:19:12.147644Z",
          "status": {
            "code": 21100,
            "description": "Model trained successfully"
          },
          "train_stats": {}
        },
        "display_name": "General"
      },
      "input": {
        "id": "@inputID1",
        "data": {
          "image": {
            "url": "https://clarifai.com/developer/static/images/model-samples/celeb-001.jpg"
          }
        }
      },
      "data": {
        "concepts": [
          {
            "id": "@conceptID11",
            "name": "menschen",
            "value": 0.9963381,
            "app_id": "main"
          },
          {
            "id": "@conceptID12",
            "name": "ein",
            "value": 0.9879057,
            "app_id": "main"
          }
        ]
      }
    },
    {
      "id": "@outputID2",
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "created_at": "2019-01-29T16:45:43.793810775Z",
      "model": {
        "id": "aaa03c23b3724a16a56b629203edc62c",
        "name": "general",
        "created_at": "2016-03-09T17:11:39.608845Z",
        "app_id": "main",
        "output_info": {
          "message": "Show output_info with: GET /models/{model_id}/output_info",
          "type": "concept",
          "type_ext": "concept"
        },
        "model_version": {
          "id": "aa9ca48295b37401f8af92ad1af0d91d",
          "created_at": "2016-07-13T01:19:12.147644Z",
          "status": {
            "code": 21100,
            "description": "Model trained successfully"
          },
          "train_stats": {}
        },
        "display_name": "General"
      },
      "input": {
        "id": "@inputID2",
        "data": {
          "image": {
            "url": "https://clarifai.com/developer/static/images/model-samples/apparel-001.jpg"
          }
        }
      },
      "data": {
        "concepts": [
          {
            "id": "@conceptID21",
            "name": "brillen und kontaktlinsen",
            "value": 0.99984586,
            "app_id": "main"
          },
          {
            "id": "@conceptID22",
            "name": "linse",
            "value": 0.999823,
            "app_id": "main"
          }
        ]
      }
    }
  ]
}
""")

    app = ClarifaiApp()
    model = app.models.get(model_id='@modelID')
    response = model.predict(
        [Image(url='@url1'), Image(url='@url2')],
        ModelOutputInfo(output_config=ModelOutputConfig(
            language='de', max_concepts=2, min_value=0.5)))

    output1 = response['outputs'][0]
    assert output1['input']['id'] == '@inputID1'
    assert output1['id'] == '@outputID1'
    assert output1['data']['concepts'][0]['id'] == '@conceptID11'
    assert output1['data']['concepts'][1]['id'] == '@conceptID12'

    output2 = response['outputs'][1]
    assert output2['input']['id'] == '@inputID2'
    assert output2['id'] == '@outputID2'
    assert output2['data']['concepts'][0]['id'] == '@conceptID21'
    assert output2['data']['concepts'][1]['id'] == '@conceptID22'

    assert_request(
        mock_execute_request, 'POST', '/v2/models/@modelID/outputs', """
{
  "inputs": [
    {
      "data": {
        "image": {
          "url": "@url1"
        }
      }
    },
    {
      "data": {
        "image": {
          "url": "@url2"
        }
      }
    }
  ],
  "model": {
    "output_info": {
      "output_config": {
        "language": "de",
        "max_concepts": 2,
        "min_value": 0.5
      }
    }
  }
}
  """)
def test_concept_bulk_predict(mock_http_client):  # type: (mock.Mock) -> None
    mock_execute_request = mock_request(
        mock_http_client, """
{
  "status": {
    "code": 10000,
    "description": "Ok"
  },
  "outputs": [{
    "id": "@outputID1",
    "status": {
      "code": 10000,
      "description": "Ok"
    },
    "created_at": "2017-11-17T19:32:58.760477937Z",
    "model": {
      "id": "@modelID1",
      "name": "@modelName1",
      "created_at": "2016-03-09T17:11:39.608845Z",
      "app_id": "main",
      "output_info": {
        "message": "Show output_info with: GET /models/{model_id}/output_info",
        "type": "concept",
        "type_ext": "concept"
      },
      "model_version": {
        "id": "@modelVersionID1",
        "created_at": "2016-07-13T01:19:12.147644Z",
        "status": {
          "code": 21100,
          "description": "Model trained successfully"
        }
      },
      "display_name": "@modelDisplayName1"
    },
    "input": {
      "id": "@inputID1",
      "data": {
        "image": {
          "url": "@imageUrl1"
        }
      }
    },
    "data": {
      "concepts": [{
        "id": "@conceptID11",
        "name": "@conceptName11",
        "value": 0.99,
        "app_id": "main"
      }, {
        "id": "@conceptID12",
        "name": "@conceptName12",
        "value": 0.98,
        "app_id": "main"
      }]
    }
  },
  {
    "id": "@outputID2",
    "status": {
      "code": 10000,
      "description": "Ok"
    },
    "created_at": "2017-11-17T19:32:58.760477937Z",
    "model": {
      "id": "@modelID2",
      "name": "@modelName2",
      "created_at": "2016-03-09T17:11:39.608845Z",
      "app_id": "main",
      "output_info": {
        "message": "Show output_info with: GET /models/{model_id}/output_info",
        "type": "concept",
        "type_ext": "concept"
      },
      "model_version": {
        "id": "@modelVersionID2",
        "created_at": "2016-07-13T01:19:12.147644Z",
        "status": {
          "code": 21100,
          "description": "Model trained successfully"
        }
      },
      "display_name": "@modelDisplayName2"
    },
    "input": {
      "id": "@inputID2",
      "data": {
        "image": {
          "url": "@imageUrl2"
        }
      }
    },
    "data": {
      "concepts": [{
        "id": "@conceptID21",
        "name": "@conceptName21",
        "value": 0.99,
        "app_id": "main"
      }, {
        "id": "@conceptID22",
        "name": "@conceptName22",
        "value": 0.98,
        "app_id": "main"
      }]
    }
  }]
}
""")

    app = ClarifaiApp()
    model = app.models.get(model_id='@modelID')
    response = model.predict([Image(url='@url1'), Image(url='@url2')])

    output1 = response['outputs'][0]
    assert output1['input']['id'] == '@inputID1'
    assert output1['id'] == '@outputID1'
    assert output1['data']['concepts'][0]['id'] == '@conceptID11'
    assert output1['data']['concepts'][1]['id'] == '@conceptID12'

    output2 = response['outputs'][1]
    assert output2['input']['id'] == '@inputID2'
    assert output2['id'] == '@outputID2'
    assert output2['data']['concepts'][0]['id'] == '@conceptID21'
    assert output2['data']['concepts'][1]['id'] == '@conceptID22'

    assert_request(
        mock_execute_request, 'POST', '/v2/models/@modelID/outputs', """
{
  "inputs": [
    {
      "data": {
        "image": {
          "url": "@url1"
        }
      }
    },
    {
      "data": {
        "image": {
          "url": "@url2"
        }
      }
    }
  ]
}
  """)
Exemple #18
0
import json

app_id = "-Vi-qtSkO7YoquYqPUbjR-In9G74Y7TYoAyQsnA5"
app_secret = "VaVyBbyvmnpoyvOehbDjsnh284T7Nx751LkSVd5H"

## API Client
clarifai_api = ClarifaiApi()

####################################################################################################

## create our endpoint
##Debug: print "Recieving access token:\n "
api = ApiClient(client_id=app_id, client_secret=app_secret)
######################################## sample images for debuggin  #################################
img1 = Image(
    url=
    'https://www.royalcanin.com/~/media/Royal-Canin/Product-Categories/cat-adult-landing-hero.ashx',
    labels=['cat'])
img2 = Image(
    url=
    'http://static.independent.co.uk/s3fs-public/styles/article_large/public/thumbnails/image/2016/02/25/13/cat-getty_0.jpg',
    labels=['cat'])
img3 = Image(url='http://www.lafayettecountyhealth.org/ThreePeople.jpg')

######################################## images for a programmer #####################################
computer_img1 = Image(
    url='https://static.pexels.com/photos/6218/desk-laptop-notebook-table.jpg',
    labels=['computer'])
computer_img2 = Image(
    url=
    'http://www.encouragersoftware.com/wp-content/uploads/2015/10/laptop-size.jpeg',
    labels=['computer'])
Exemple #19
0
def test_bulk_create_image(mock_http_client):  # type: (mock.Mock) -> None
  mock_execute_request = mock_request(
      mock_http_client, """
{
  "status": {
    "code": 10000,
    "description": "Ok"
  },
  "inputs": [
    {
      "id": "@inputID1",
      "data": {
        "image": {
          "url": "https://some.image.url1"
        },
        "geo": {
          "geo_point": {
            "longitude": 55,
            "latitude": 66
          }
        }
      },
      "created_at": "2019-01-17T12:43:04.895006174Z",
      "modified_at": "2019-01-17T12:43:04.895006174Z",
      "status": {
        "code": 30001,
        "description": "Download pending"
      }
    },
    {
      "id": "@inputID2",
      "data": {
        "image": {
          "url": "https://some.image.url2"
        }
      },
      "created_at": "2019-01-17T12:43:04.895006174Z",
      "modified_at": "2019-01-17T12:43:04.895006174Z",
      "status": {
        "code": 30001,
        "description": "Download pending"
      }
    }
  ]
}
""")

  app = ClarifaiApp()
  images = app.inputs.bulk_create_images([
      Image(
          url='https://some.image.url1',
          image_id='@inputID1',
          allow_dup_url=True,
          geo=Geo(geo_point=GeoPoint(55, 66))),
      Image(url='https://some.image.url2', image_id='@inputID2', allow_dup_url=True),
  ])

  assert images[0].input_id == '@inputID1'
  assert images[0].url == 'https://some.image.url1'
  assert images[0].geo.geo_point.longitude == 55
  assert images[0].geo.geo_point.latitude == 66
  assert images[0].status.code == 30001

  assert images[1].input_id == '@inputID2'
  assert images[1].url == 'https://some.image.url2'
  assert not images[1].geo
  assert images[1].status.code == 30001

  assert_request(
      mock_execute_request, 'POST', '/v2/inputs', """
{
  "inputs": [
    {
      "id": "@inputID1",
      "data": {
        "image": {
          "url": "https://some.image.url1",
          "allow_duplicate_url": true
        },
        "geo": {
          "geo_point": {
            "longitude": 55,
            "latitude": 66
          }
        }
      }
    },
    {
      "id": "@inputID2",
      "data": {
        "image": {
          "url": "https://some.image.url2",
          "allow_duplicate_url": true
        }
      }
    }
  ]
}
        """)
Exemple #20
0
def test_bulk_update_with_regions(mock_http_client):  # type: (mock.Mock) -> None
  mock_execute_request = mock_request(
      mock_http_client, """
{
  "status": {
    "code": 10000,
    "description": "Ok"
  },
  "inputs": [
    {
      "id": "@inputID",
      "data": {
        "image": {
          "url": "@imageURL"
        },
        "concepts": [
          {
            "id": "@concept1",
            "name": "@concept1",
            "value": 1,
            "app_id": "@appID"
          },
          {
            "id": "@concept2",
            "name": "@concept2",
            "value": 0,
            "app_id": "@appID"
          }
        ]
      },
      "created_at": "2019-01-29T15:23:21.188492Z",
      "modified_at": "2019-01-29T15:23:21.575667Z",
      "status": {
        "code": 30200,
        "description": "Input image modification success"
      }
    }
  ]
}
""")

  app = ClarifaiApp()
  images = app.inputs.bulk_update(
      [
          Image(
              image_id='@inputID',
              regions=[
                  Region(
                      region_info=RegionInfo(
                          bbox=BoundingBox(0.5, 0.5, 1.0, 1.0),
                          feedback_type=FeedbackType.misplaced),
                      concepts=[
                          Concept(concept_id='@concept1', value=True),
                          Concept(concept_id='@concept2', value=False)
                      ],
                      region_id='@regionID',
                      face=Face(
                          identity=FaceIdentity(concepts=[
                              Concept(concept_id='@faceConcept1', value=True),
                              Concept(concept_id='@faceConcept2', value=False),
                          ])))
              ])
      ],
      action='overwrite')

  assert images[0].input_id == '@inputID'
  assert images[0].url == '@imageURL'
  assert images[0].status.code == 30200

  assert_request(
      mock_execute_request, 'PATCH', '/v2/inputs', """
{
  "inputs": [
    {
      "id": "@inputID",
      "data": {
        "regions": [
          {
            "id": "@regionID",
            "region_info": {
              "bounding_box": {
                "top_row": 0.5,
                "left_col": 0.5,
                "bottom_row": 1,
                "right_col": 1
              },
              "feedback": "misplaced"
            },
            "data": {
              "concepts": [
                {
                  "id": "@concept1",
                  "value": 1
                },
                {
                  "id": "@concept2",
                  "value": 0
                }
              ],
              "face": {
                "identity": {
                  "concepts": [
                    {
                      "id": "@faceConcept1",
                      "value": 1
                    },
                    {
                      "id": "@faceConcept2",
                      "value": 0
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    }
  ],
  "action": "overwrite"
}
        """)
Exemple #21
0
def test_bulk_update(mock_http_client):  # type: (mock.Mock) -> None
  mock_execute_request = mock_request(
      mock_http_client, """
{
    "status": {
        "code": 10000,
        "description": "Ok"
    },
    "inputs": [{
        "id": "@inputID",
        "data": {
            "image": {
                "url": "@imageURL"
            },
            "concepts": [
                {
                  "id": "@positiveConcept1",
                  "name": "@positiveConceptName1",
                  "value": 1
                },
                {
                  "id": "@positiveConcept2",
                  "value": 1
                },
                {
                  "id": "@negativeConcept1",
                  "name": "@negativeConceptName1",
                  "value": 0
                },
                {
                  "id": "@negativeConcept2",
                  "value": 0
                }
            ]
        },
        "created_at": "2017-10-13T20:53:00.253139Z",
        "modified_at": "2017-10-13T20:53:00.868659782Z",
        "status": {
            "code": 30200,
            "description": "Input image modification success"
        }
    }]
}
""")

  app = ClarifaiApp()
  images = app.inputs.bulk_update([
      Image(
          image_id='@inputID',
          concepts=['@positiveConcept1', '@positiveConcept2'],
          not_concepts=['@negativeConcept1', '@negativeConcept2']),
  ])

  assert images[0].input_id == '@inputID'
  assert images[0].url == '@imageURL'
  assert images[0].status.code == 30200

  assert_request(
      mock_execute_request, 'PATCH', '/v2/inputs', """
{
  "inputs": [
    {
      "id": "@inputID",
      "data": {
        "concepts": [
          {
            "id": "@positiveConcept1",
            "value": 1
          },
          {
            "id": "@positiveConcept2",
            "value": 1
          },
          {
            "id": "@negativeConcept1",
            "value": 0
          },
          {
            "id": "@negativeConcept2",
            "value": 0
          }
        ]
      }
    }
  ],
  "action":"merge"
}
        """)
import os

app = ClarifaiApp(os.environ['CLARIFAI_APP_ID'],
                  os.environ['CLARIFAI_APP_SECRET'])

try:
    trained_model = app.models.get('form')
except client.ApiError:
    path_to_training_images = 'images/training/'
    path_to_valid_images = path_to_training_images + 'valid/'
    path_to_invalid_images = '%sinvalid/' % path_to_training_images
    valid_image_filenames = os.listdir(path_to_valid_images)

    training_images = [
        Image(filename=path_to_invalid_images + 'invalid.png',
              concepts=['invalid'],
              not_concepts=['valid'])
    ]

    for valid_image_filename in valid_image_filenames:
        path_to_valid_image = path_to_valid_images + valid_image_filename
        valid_image = Image(
            filename=path_to_valid_image,
            concepts=['valid'],
            not_concepts=['invalid'],
        )

        training_images.append(valid_image)

    app.inputs.bulk_create_images(training_images)
Exemple #23
0
    def test_predict_model(self):
        """ test predict with general model """

        model = self.app.models.get('general-v1.3')
        image = Image(url=urls[0])
        model.predict([image])
Exemple #24
0
def upload():
    # Get the name of the uploaded file
    file = request.files['file']
    username = request.form['username']
    data = api.getData(username)
    follows = data["follows"]["count"]
    followers = data["followed_by"]["count"]
    # Check if the file is one of the allowed types/extensions
    if file and allowed_file(file.filename):
        # Make the filename safe, remove unsupported chars
        filename = secure_filename(file.filename)
        # Move the file form the temporal folder to
        # the upload folder we setup
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # Redirect the user to the uploaded_file route, which
        # will basicaly show on the browser the uploaded file
        image = ClImage(file_obj=open('uploads/' + filename, 'rb'))
        results = []
        classes = []
        probs = []
        imageinfo = model.predict([image])
        for i in imageinfo['outputs'][0]['data']['concepts']:
            classes.append(i['name'])
            probs.append(i['value'])
        # results.append({'result': {'tag': {'classes': classes, 'probs': probs}}})
        tag_pool = []
        # for result in results:
        # result = result["result"]["tag"]
        # tag_pool.extend(result["classes"])
        # users_omega['naimmiah08'] = results #needs to be changed for username
        getTags = db.tags_pool.find()
        for tags in getTags:
            # print tags['tags']
            tag_pool.extend(tags['tags'])
        tag_pool = set(tag_pool)
        # db.tags_pool.update({'id': 1}, { '$set' : {'tags': list(tag_pool)}})
        image = []
        for tag in tag_pool:
            if tag in classes:
                image.append(1)
                idx = classes.index(tag)
                image.append(probs[idx])
            else:
                image.append(0)
                image.append(0)
        # print follows, followers
        image.append(follows >= follows_median)
        image.append(followers >= followers_median)
        likes = linearClassifier.predict(image)
        # print len(image)
        # print likes
        # for user in users_omega:
        # data = api.getData(user)
        # follows = data["follows"]["count"]
        # followed_by = data["followed_by"]["count"]
        # bio = data["biography"]
        # media = api.getPictures(user)
        # media = data["media"]["nodes"]
        # results = users_omega[user]
        # i = 0
        # for result in results:
        # features = []
        # result = result["result"]["tag"]
        # item = media[i]
        # likes.append(item["likes"]["count"])
        # caption = item["caption"]
        # classes = result["classes"]
        # probs = result["probs"]
        # for tag in tag_pool:
        # if tag in classes:
        # features.append(1)
        # idx = classes.index(tag)
        # features.append(probs[idx])
        # else:
        # features.append(0)
        # features.append(0)
        # features.append(follows)
        # features.append(followed_by)
        # following.append(follows)
        # followers.append(followed_by)
        # i = i + 1
        # images.append(features)
        # images = dataset[0]
        # likes = dataset[1]
        # print images
        # print likes
        return render_template("result.html",
                               image=filename,
                               username=username,
                               likes=ceil(likes[0]))
    return "jam ciemny jest wschrod"
Exemple #25
0
def test_bulk_update_with_metadata(mock_http_client):  # type: (mock.Mock) -> None
  mock_execute_request = mock_request(
      mock_http_client, """
{
    "status": {
        "code": 10000,
        "description": "Ok"
    },
    "inputs": [{
        "id": "@inputID",
        "data": {
            "image": {
                "url": "@imageURL"
            },
            "concepts": [{
                "id": "concept1",
                "name": "concept1",
                "value": 1,
                "app_id": "@appID"
            }],
            "metadata": {
                "@key1": "@value1",
                "@key2": "@value2"
            }
        },
        "created_at": "2017-11-02T15:08:22.005157Z",
        "modified_at": "2017-11-02T15:08:23.071624222Z",
        "status": {
            "code": 30200,
            "description": "Input image modification success"
        }
    }]
}
""")

  app = ClarifaiApp()
  images = app.inputs.bulk_update(
      [Image(image_id='@inputID', metadata={
          '@key1': '@value1',
          '@key2': '@value2',
      })],
      action='overwrite')

  assert images[0].input_id == '@inputID'
  assert images[0].url == '@imageURL'
  assert images[0].status.code == 30200
  assert images[0].metadata == {'@key1': '@value1', '@key2': '@value2'}

  assert_request(
      mock_execute_request, 'PATCH', '/v2/inputs', """
{
    "inputs": [
      {
        "id": "@inputID",
        "data": {
          "metadata": {
            "@key1": "@value1",
            "@key2": "@value2"
          }
        }
      }
    ],
    "action":"overwrite"
}
        """)
def test_predict_multiple_with_one_invalid_url(
        mock_http_client):  # type: (mock.Mock) -> None
    mock_execute_request = mock_request(
        mock_http_client, """
{
  "status": {
    "code": 10010,
    "description": "Mixed Success"
  },
  "outputs": [
    {
      "id": "@outputID1",
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "created_at": "2019-01-20T20:25:20.302505245Z",
      "model": {
        "id": "@modelID",
        "name": "general",
        "created_at": "2016-03-09T17:11:39.608845Z",
        "app_id": "main",
        "output_info": {
          "message": "Show output_info with: GET /models/{model_id}/output_info",
          "type": "concept",
          "type_ext": "concept"
        },
        "model_version": {
          "id": "@modelVersionID",
          "created_at": "2016-07-13T01:19:12.147644Z",
          "status": {
            "code": 21100,
            "description": "Model trained successfully"
          },
          "train_stats": {}
        },
        "display_name": "General"
      },
      "input": {
        "id": "@inputID1",
        "data": {
          "image": {
            "url": "@validURL"
          }
        }
      },
      "data": {
        "concepts": [
          {
            "id": "@concept1",
            "name": "people",
            "value": 0.9963381,
            "app_id": "main"
          },
          {
            "id": "@concept2",
            "name": "one",
            "value": 0.9879057,
            "app_id": "main"
          }
        ]
      }
    },
    {
      "id": "@outputID2",
      "status": {
        "code": 30002,
        "description": "Download failed; check URL",
        "details": "404 Client Error: Not Found for url: @invalidURL"
      },
      "created_at": "2019-01-20T20:25:20.302505245Z",
      "model": {
        "id": "@modelID",
        "name": "general",
        "created_at": "2016-03-09T17:11:39.608845Z",
        "app_id": "main",
        "output_info": {
          "message": "Show output_info with: GET /models/{model_id}/output_info",
          "type": "concept",
          "type_ext": "concept"
        },
        "model_version": {
          "id": "@modelVersionID",
          "created_at": "2016-07-13T01:19:12.147644Z",
          "status": {
            "code": 21100,
            "description": "Model trained successfully"
          },
          "train_stats": {}
        },
        "display_name": "General"
      },
      "input": {
        "id": "@inputID2",
        "data": {
          "image": {
            "url": "@invalidURL"
          }
        }
      },
      "data": {}
    }
  ]
}
""")

    app = ClarifaiApp()
    model = app.public_models.general_model
    response = model.predict(
        [Image(url='@validURL'),
         Image(url='@invalidURL')])

    assert response['status']['code'] == 10010
    assert response['status']['description'] == 'Mixed Success'

    assert_request(
        mock_execute_request, 'POST',
        '/v2/models/' + model.model_id + '/outputs', """
{
  "inputs": [
    {
      "data": {
        "image": {
          "url": "@validURL"
        }
      }
    },
    {
      "data": {
        "image": {
          "url": "@invalidURL"
        }
      }
    }
  ]
}
        """)
Exemple #27
0
                    help="Input images",
                    default='')

args = parser.parse_args()

inputImgName = args.input.split(',')[0:-1]

app = ClarifaiApp(api_key='9295b11b259b45dbbfc7df8159745e74')

model = app.models.get('general-v1.3')

# images = []
keywords = []
for imgName in inputImgName:
    print(imgName)
    image = ClImage(file_obj=open('public/images/' + imgName, 'rb'))
    data = model.predict([image])
    keywords += [
        name['name'] for name in data['outputs'][0]['data']['concepts']
    ][:3]

print(keywords)
keyword_string = " ".join(keywords)
with open('public/keywords.txt', 'w') as txt_file:
    txt_file.write(keyword_string)

keyword_string += ' food recipe'
print(keyword_string)
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(chrome_options=options)
Exemple #28
0
def test_bulk_workflow_predict_with_url(
        mock_http_client):  # type: (mock.Mock) -> None
    mock_execute_request = mock_request(mock_http_client,
                                        json_response="""
{
  "status": {
    "code": 10000,
    "description": "Ok"
  },
  "workflow": {
    "id": "@workflowID",
    "app_id": "@appID",
    "created_at": "2017-06-15T15:17:30.462323Z"
  },
  "results": [
    {
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "input": {
        "id": "@inputID1",
        "data": {
          "image": {
            "url": "@url1"
          }
        }
      },
      "outputs": [
        {
          "id": "@outputID11",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736685542Z",
          "model": {
            "id": "@modelID1",
            "name": "food-items-v1.0",
            "created_at": "2016-09-17T22:18:59.955626Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersionID1",
              "created_at": "2016-09-17T22:18:59.955626Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "Food"
          },
          "data": {}
        },
        {
          "id": "@outputID12",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736712374Z",
          "model": {
            "id": "@modelID2",
            "name": "general",
            "created_at": "2016-03-09T17:11:39.608845Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersion2",
              "created_at": "2016-07-13T01:19:12.147644Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "General"
          },
          "data": {
            "concepts": [
              {
                "id": "@conceptID11",
                "name": "people",
                "value": 0.9963381,
                "app_id": "main"
              },
              {
                "id": "@conceptID12",
                "name": "one",
                "value": 0.9879056,
                "app_id": "main"
              },
              {
                "id": "@conceptID13",
                "name": "portrait",
                "value": 0.9849082,
                "app_id": "main"
              }
            ]
          }
        }
      ]
    },
    {
      "status": {
        "code": 10000,
        "description": "Ok"
      },
      "input": {
        "id": "@inputID2",
        "data": {
          "image": {
            "url": "@url2"
          }
        }
      },
      "outputs": [
        {
          "id": "@outputID21",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736685542Z",
          "model": {
            "id": "@modelID1",
            "name": "food-items-v1.0",
            "created_at": "2016-09-17T22:18:59.955626Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersion1",
              "created_at": "2016-09-17T22:18:59.955626Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "Food"
          },
          "data": {
            "concepts": [
              {
                "id": "@concept21",
                "name": "spatula",
                "value": 0.9805687,
                "app_id": "main"
              }
            ]
          }
        },
        {
          "id": "@outputID22",
          "status": {
            "code": 10000,
            "description": "Ok"
          },
          "created_at": "2019-01-29T17:36:23.736712374Z",
          "model": {
            "id": "@modelID2",
            "name": "general",
            "created_at": "2016-03-09T17:11:39.608845Z",
            "app_id": "main",
            "output_info": {
              "message": "Show output_info with: GET /models/{model_id}/output_info",
              "type": "concept",
              "type_ext": "concept"
            },
            "model_version": {
              "id": "@modelVersion2",
              "created_at": "2016-07-13T01:19:12.147644Z",
              "status": {
                "code": 21100,
                "description": "Model trained successfully"
              },
              "train_stats": {}
            },
            "display_name": "General"
          },
          "data": {
            "concepts": [
              {
                "id": "@conceptID31",
                "name": "eyewear",
                "value": 0.99984586,
                "app_id": "main"
              },
              {
                "id": "@conceptID32",
                "name": "lens",
                "value": 0.999823,
                "app_id": "main"
              },
              {
                "id": "@conceptID33",
                "name": "eyeglasses",
                "value": 0.99980056,
                "app_id": "main"
              }
            ]
          }
        }
      ]
    }
  ]
}
""")

    app = ClarifaiApp()

    workflow = Workflow(app.api, workflow_id='@workflowID')
    response = workflow.predict(
        [Image(url='@url1'), Image(url='@url2')],
        ModelOutputConfig(min_value=0.5, max_concepts=3))

    assert response['workflow']['id'] == '@workflowID'
    assert response['results'][0]['outputs'][1]['data']['concepts'][0][
        'id'] == '@conceptID11'

    assert_request(
        mock_execute_request, 'POST', '/v2/workflows/@workflowID/results', """
{
  "inputs": [
    {
      "data": {
        "image": {
          "url": "@url1"
        }
      }
    },
    {
      "data": {
        "image": {
          "url": "@url2"
        }
      }
    }
  ],
  "output_config": {
    "max_concepts": 3,
    "min_value": 0.5
  }
}
  """)
Exemple #29
0
def identify(numid=-1, matchargs_refresh=False, clarifai_refresh=False):
    """
    clarifai_refresh=True to call clarifai all over again.
    matchargs_refresh=True recreates identification but does not call clarifai
    numid process numid only url
    """
    url_list = []
    for urlobj in urls:
        url = urlobj['url']
        if clarifai_refresh:
            url_list.append(url)
        else:
            try:
                faceimageurl = FaceImageUrls.get(url)
                if DEBUG: print('get succeeded. already have identification.')
            except:
                url_list.append(url)
                if DEBUG: print('get failed. calling clarifai for url', url)
    # batch the clarifai call
    if len(url_list):
        if DEBUG:
            print('calling Clarifai now for {} images'.format(len(url_list)))
        res1 = workflow.predict([Image(url) for url in url_list])
        res2 = [identify1(_res) for _res in res1['results']]
        res3 = dict(zip(url_list, res2))
    for urlobj in urls:
        url = urlobj['url']
        try:
            faceimageurl = FaceImageUrls.get(url)
            if clarifai_refresh or matchargs_refresh:
                faceimageurl.identification = None
        except:
            faceimageurl = FaceImageUrls(url)
            faceimageurl.numid = FaceImageUrls.count() + 1
            if DEBUG:
                print('creating new FaceImageUrl: {} {} {}'.format(
                    faceimageurl.numid, faceimageurl.description,
                    faceimageurl.url))
        if faceimageurl.identification == None:
            faceimageurl.identification = res3[url]
        faceimageurl.description = urlobj['desc']
        faceimageurl.save()
    # all urls should be identified now
    for urlobj in urls:
        url = urlobj['url']
        try:
            faceimageurl = FaceImageUrls.get(url)  # should not fail
        except:
            print('get failed. should not have failed for url', url)
        urlobj['faceimageurl'] = faceimageurl
        if not numid == -1 and not numid == faceimageurl.numid:
            continue
        print(faceimageurl.numid, faceimageurl.description, url)
        #if DEBUG: pprint.pprint(faceimageurl.identification.as_dict())
        match_args = get_match_args(faceimageurl.identification)
        args = ['items', 'output2', '--numid', str(faceimageurl.numid)]
        for key, value in match_args.items():
            args.append(key)
            if not type(value) is bool:
                args.append(value)
        if DEBUG: print('before:', match_args)
        if DEBUG: print('after:', args)
        try:
            run_match3.run_random(10, args)
        except Exception as ex:
            print("Failed to create images for URL. errmsg:", ex)

    print()
    for urlobj in urls:
        print('{numid} {description}'.format(
            numid=urlobj['faceimageurl'].numid,
            description=urlobj['faceimageurl'].description))