96 lines
2.2 KiB
Bash
Executable File
96 lines
2.2 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Example curl client for the Human Pose Estimation server.
|
|
#
|
|
# Usage:
|
|
# ./client_example.sh <image_path> [url]
|
|
#
|
|
# Examples:
|
|
# ./client_example.sh photo.jpg
|
|
# ./client_example.sh photo.png https://api.pose.weihua-iot.cn/hpe
|
|
#
|
|
|
|
set -euo pipefail
|
|
|
|
# Default server URL
|
|
DEFAULT_URL="https://api.pose.weihua-iot.cn/hpe"
|
|
|
|
# Parse arguments
|
|
if [[ $# -lt 1 ]]; then
|
|
echo "Usage: $0 <image_path> [url]" >&2
|
|
echo "" >&2
|
|
echo "Examples:" >&2
|
|
echo " $0 photo.jpg" >&2
|
|
echo " $0 photo.png http://192.168.1.100:8245/hpe" >&2
|
|
exit 1
|
|
fi
|
|
|
|
IMAGE_PATH="$1"
|
|
URL="${2:-$DEFAULT_URL}"
|
|
|
|
# Check if image exists
|
|
if [[ ! -f "$IMAGE_PATH" ]]; then
|
|
echo "Error: Image file not found: $IMAGE_PATH" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Determine content type from file extension
|
|
get_content_type() {
|
|
local ext="${1##*.}"
|
|
ext="${ext,,}" # lowercase
|
|
case "$ext" in
|
|
jpg|jpeg)
|
|
echo "image/jpeg"
|
|
;;
|
|
png)
|
|
echo "image/png"
|
|
;;
|
|
*)
|
|
echo "Error: Unsupported image format: .$ext. Use JPEG or PNG." >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
CONTENT_TYPE=$(get_content_type "$IMAGE_PATH")
|
|
|
|
# Send request and capture response
|
|
echo "Sending image to $URL ..." >&2
|
|
|
|
HTTP_RESPONSE=$(curl -s -w "\n%{http_code}" \
|
|
-X POST \
|
|
-H "Content-Type: $CONTENT_TYPE" \
|
|
--data-binary "@$IMAGE_PATH" \
|
|
"$URL")
|
|
|
|
# Split response body and status code
|
|
HTTP_BODY=$(echo "$HTTP_RESPONSE" | sed '$d')
|
|
HTTP_CODE=$(echo "$HTTP_RESPONSE" | tail -n1)
|
|
|
|
# Handle response
|
|
case "$HTTP_CODE" in
|
|
200)
|
|
if [[ -z "$HTTP_BODY" ]]; then
|
|
echo "No poses detected in the image."
|
|
else
|
|
# Pretty print JSON if jq is available
|
|
if command -v jq &> /dev/null; then
|
|
echo "$HTTP_BODY" | jq .
|
|
else
|
|
echo "$HTTP_BODY"
|
|
fi
|
|
fi
|
|
;;
|
|
*)
|
|
echo "HTTP Error: $HTTP_CODE" >&2
|
|
if [[ -n "$HTTP_BODY" ]]; then
|
|
if command -v jq &> /dev/null; then
|
|
echo "$HTTP_BODY" | jq . >&2
|
|
else
|
|
echo "$HTTP_BODY" >&2
|
|
fi
|
|
fi
|
|
exit 1
|
|
;;
|
|
esac
|