CoolFace
Apppublic

SE-09/HapticsProject

sourceHugging Faceunknownupdated 2y agoView on Hugging Face
2likes
apiTest.py145 linesDownload Raw Back to root
1import requests2 3index_counter = 0  # Global counter for video indexes4 5def videoAnalysis(sas_token, sas_url, search):6    7    global index_counter  # Access the global counter variable8 9    index_counter += 110    index_name = f"video-index-{index_counter}"11 12    url = f"https://explosion.cognitiveservices.azure.com/computervision/retrieval/indexes/{index_name}?api-version=2023-05-01-preview"13    headers = {14        "Ocp-Apim-Subscription-Key": "c54eec632ae5413e8075e3f825727822",15        "Content-Type": "application/json"16    }17 18    data = {19        "metadataSchema": {20            "fields": [21                {22                    "name": "cameraId",23                    "searchable": False,24                    "filterable": True,25                    "type": "string"26                },27                {28                    "name": "timestamp",29                    "searchable": False,30                    "filterable": True,31                    "type": "datetime"32                }33            ]34        },35        "features": [36            {37                "name": "vision",38                "domain": "surveillance"39            },40            {41                "name": "speech"42            }43        ]44    }45 46 47    response = requests.put(url, json=data, headers=headers)48    49    #test code for errors50    print("Response Status Code:", response.status_code)51    print("Response Content:", response.text)52 53 54    url_index = f"https://explosion.cognitiveservices.azure.com/computervision/retrieval/indexes/{index_name}/ingestions/my-ingestion?api-version=2023-05-01-preview"55    headers = {56        "Ocp-Apim-Subscription-Key": "c54eec632ae5413e8075e3f825727822",57        "Content-Type": "application/json"58    }59 60 61    data_index = {62        "videos": [63            {64                "mode": "add",65                "documentId": sas_token,66                "documentUrl": sas_url,67                "metadata": {68                    "cameraId": "camera1",69                    "timestamp": "2024-02-09 00:02:14"70                }71            }72        ]73    }74 75    response_index = requests.put(url_index, json=data_index, headers=headers)76 77    #test code for errors78    print("Index Ingestion - Response Status Code:", response_index.status_code)79    print("Index Ingestion - Response Content:", response_index.text)80 81    # Assuming you want to ingest another video with a different ingestion name82    url_new_ingestion = f"https://explosion.cognitiveservices.azure.com/computervision/retrieval/indexes/{index_name}/ingestions/new-ingestion?api-version=2023-05-01-preview"83 84    data_new_ingestion = {85        "videos": [86            {87                "mode": "add",88                "documentId": "new_document_id",89                "documentUrl": "https://example.blob.core.windows.net/videos/new_video.mp4?sas_token_here",90                "metadata": {91                    "cameraId": "camera3"92                }93            }94        ]95    }96 97    response_new_ingestion = requests.put(url_new_ingestion, json=data_new_ingestion, headers=headers)98 99    #used to test code for errors100    print("New Ingestion - Response Status Code:", response_new_ingestion.status_code)101    print("New Ingestion - Response Content:", response_new_ingestion.text)102 103 104    url_query = f"https://explosion.cognitiveservices.azure.com/computervision/retrieval/indexes/{index_name}:queryByText?api-version=2023-05-01-preview"105    headers = {106        "Ocp-Apim-Subscription-Key": "c54eec632ae5413e8075e3f825727822",107        "Content-Type": "application/json"108    }109 110    data_query = {111        "queryText": search,112        "filters": {113            "stringFilters": [114                {115                    "fieldName": "cameraId",116                    "values": [117                        "camera1"118                    ]119                }120            ],121            "featureFilters": ["vision"]122        }123    }124 125    response_query = requests.post(url_query, json=data_query, headers=headers)126 127    if response_query.text == """{"error":{"code":"InvalidRequest","message":"Value for indexName is invalid."}}""":128        videoAnalysis(sas_token,sas_url, search)    129    else: 130        return response_query.text131 132#Enter the sas token133sas_token_1 = "sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D"134#the sas url135sas_url_1= "https://store1video.blob.core.windows.net/haptic-vid/test_video.mp4?sp=r&st=2024-02-09T12:33:24Z&se=2025-08-06T20:33:24Z&spr=https&sv=2022-11-02&sr=b&sig=V%2Fq56JjGcL60r0vt3oAPjzx%2FZMu5%2BJo%2BfjKkJF2ccgo%3D"136#query of what you are looking for 137instance_1 = "Explosion"138 139#Enter the sas token140sas_token_2 = "sp=r&st=2024-03-18T09:48:32Z&se=2026-07-02T17:48:32Z&spr=https&sv=2022-11-02&sr=b&sig=hLiFrDUtrutW9FWWRR7Z0Kbc4wkHs28YR9RXjVxw8uc%3D"141#the sas url142sas_url_2= "https://store1video.blob.core.windows.net/haptic-vid/desert_vehicle_test.mp4?sp=r&st=2024-03-18T09:48:32Z&se=2026-07-02T17:48:32Z&spr=https&sv=2022-11-02&sr=b&sig=hLiFrDUtrutW9FWWRR7Z0Kbc4wkHs28YR9RXjVxw8uc%3D"143#query of what you are looking for 144instance_2 = "Vehicle racing" 145